kindred 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +316 -0
- data/Rakefile +2 -0
- data/app/assets/javascripts/app.coffee +1 -0
- data/app/assets/javascripts/kindred.js +13 -0
- data/app/assets/javascripts/models/active_page.coffee +97 -0
- data/app/assets/javascripts/models/base.coffee +153 -0
- data/app/assets/javascripts/models/setup.coffee +97 -0
- data/app/assets/javascripts/utilities/binder.coffee +29 -0
- data/app/assets/javascripts/utilities/listener.coffee +74 -0
- data/app/assets/javascripts/utilities/logger.coffee +12 -0
- data/app/assets/javascripts/utilities/stack_trace.js +511 -0
- data/app/assets/javascripts/utilities/template.coffee +3 -0
- data/app/assets/javascripts/utilities/uuid.coffee +9 -0
- data/app/assets/javascripts/utilities/virtual_class.coffee +25 -0
- data/app/helpers/template_helper.rb +59 -0
- data/kindred.gemspec +26 -0
- data/lib/kindred/engine.rb +8 -0
- data/lib/kindred/version.rb +3 -0
- data/lib/kindred.rb +5 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 98d0f43a8b8e5ab779ef396a9efc3c0fa3876075
|
4
|
+
data.tar.gz: d4aaa6ad2ee96a901c1a8da7a02816d0b34301f5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1bfe9d5dfb80638e4f3ff0cf5dbd9437d08449b1b83d09ddb975fa75d9185764ff0daaaf16ebd3410c696280a6b53388524847f0ead0cfb9daca62f9d78f62f
|
7
|
+
data.tar.gz: 87f37d9b4ea4daf4c71bcb3b6a3cb14e3c7764b6f2b47fefa869bc43d3afaa1d490053cec5e79a91cd4144ede5dc3e2b22842f58392c984f48369085918d459a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Mike Piccolo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,316 @@
|
|
1
|
+
kindred
|
2
|
+
============
|
3
|
+
| Project | Gem Release |
|
4
|
+
|------------------------ | ----------------- |
|
5
|
+
| Gem name | kindred |
|
6
|
+
| License | [MIT](LICENSE.txt) |
|
7
|
+
| Version | [![Gem Version](https://badge.fury.io/rb/kindred.png)](http://badge.fury.io/rb/kindred) |
|
8
|
+
| Continuous Integration | [![Build Status](https://travis-ci.org/mfpiccolo/kindred.png?branch=master)](https://travis-ci.org/mfpiccolo/kindred)
|
9
|
+
| Test Coverage | [![Coverage Status](https://coveralls.io/repos/mfpiccolo/kindred/badge.png?branch=master)](https://coveralls.io/r/mfpiccolo/kindred?branch=coveralls)
|
10
|
+
| Grade | [![Code Climate](https://codeclimate.com/github/mfpiccolo/kindred/badges/gpa.svg)](https://codeclimate.com/github/mfpiccolo/kindred)
|
11
|
+
| Dependencies | [![Dependency Status](https://gemnasium.com/mfpiccolo/kindred.png)](https://gemnasium.com/mfpiccolo/kindred)
|
12
|
+
| Homepage | [http://mfpiccolo.github.io/kindred][homepage] |
|
13
|
+
| Documentation | [http://rdoc.info/github/mfpiccolo/kindred/frames][documentation] |
|
14
|
+
| Issues | [https://github.com/mfpiccolo/kindred/issues][issues] |
|
15
|
+
|
16
|
+
## Description
|
17
|
+
Kindred is an open source project that intends to optimize programmers happiness and productivity for client-heavy rails applications. Kindred aims to allow developers to create robust client side applications with minimal code while maintaining best practices and conventions.
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem "kindred"
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install kindred
|
34
|
+
|
35
|
+
## Getting Started with Rails
|
36
|
+
|
37
|
+
Add `gem "kindred"` to gemfile
|
38
|
+
|
39
|
+
Add `//= require kindred` to application.js manifest
|
40
|
+
|
41
|
+
Change your asset pipeline configuration to prevent uglifying of JS class names:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
Rails.application.config.assets.js_compressor = Uglifier.new(mangle: false)
|
45
|
+
```
|
46
|
+
|
47
|
+
## Demo
|
48
|
+
|
49
|
+
If you would like to see kindred in action check out [kindred-demo](https://kindred-demo.herokuapp.com/invoices/1/edit).
|
50
|
+
|
51
|
+
## Features
|
52
|
+
###Templates
|
53
|
+
In a rails view you can pass html to your javascript by using the `#template` helper method.
|
54
|
+
The `#template` method takes the following keyword arguments:
|
55
|
+
|
56
|
+
`model:` String of the javascript model name
|
57
|
+
|
58
|
+
`collection:` Collection of json serializable ruby objects
|
59
|
+
|
60
|
+
`target:` String of the data-target attribute wrapper
|
61
|
+
|
62
|
+
`&block` In the block pass html that will be used for that model
|
63
|
+
|
64
|
+
```HTML
|
65
|
+
<div data-target="line-item">
|
66
|
+
<%= template(collection: @line_items, target: "line-item", model: "line_item") do %>
|
67
|
+
<tr>
|
68
|
+
<td><%= k_text_field_tag(:line_item, :description) %></td>
|
69
|
+
<td><%= k_text_field_tag(:line_item, :qty) %></td>
|
70
|
+
<td><%= k_text_field_tag(:line_item, :price_cents) %></td>
|
71
|
+
<td><%= k_check_box_tag(:line_item, :complete) %></td>
|
72
|
+
</tr>
|
73
|
+
<% end %>
|
74
|
+
</div>
|
75
|
+
```
|
76
|
+
Templates will be available in your javascript by accessing the `App.Template` class. The `#template_info` property is an array of objects that contain both the collection and the template namespaced under the model that you passed.
|
77
|
+
|
78
|
+
If you passed a line_item you could access it with:
|
79
|
+
|
80
|
+
`li_info = App.Template.template_info["line_item"]`
|
81
|
+
|
82
|
+
You could then get access to the collection or the template by using those properties.
|
83
|
+
|
84
|
+
`li_info.template` would return the html you passed through
|
85
|
+
|
86
|
+
`li_info.collection` would return the json collection
|
87
|
+
|
88
|
+
###Controllers
|
89
|
+
In kindred, javascript controllers are a client side augmentation of your ruby controllers. They are just client side code that gets run on page load.
|
90
|
+
|
91
|
+
```coffeescript
|
92
|
+
class this.InvoicesController
|
93
|
+
@edit: ->
|
94
|
+
console.log "Run this on invoice edit view"
|
95
|
+
```
|
96
|
+
|
97
|
+
To ensure that this code is run on the client side call the js method from your view or controller:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class InvoicesController < ApplicationController
|
101
|
+
def edit
|
102
|
+
@line_items = @invoice.line_items
|
103
|
+
|
104
|
+
respond_to do |format|
|
105
|
+
format.html { js }
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
###Models
|
112
|
+
|
113
|
+
A kindred model is an object that helps the interaction between the page and the rails api.
|
114
|
+
|
115
|
+
Here is an example model:
|
116
|
+
|
117
|
+
```coffeescript
|
118
|
+
class App.LineItem extends App.Base
|
119
|
+
|
120
|
+
@route = "/line_items"
|
121
|
+
@set_class_name("LineItem")
|
122
|
+
```
|
123
|
+
|
124
|
+
####Instance Functions
|
125
|
+
|
126
|
+
#####Page functions:
|
127
|
+
|
128
|
+
`append_to_page()` This function will put the values from the model instance that is called on the page. If the element found is an input it will add it as a value. If it is not an input it will insert the value into the tag.
|
129
|
+
|
130
|
+
`dirty_from_page()` This boolean function will check all the inputs that belong to the model instance that it is being called on and check if the value has changed since it was set on the page. Returns true or false.
|
131
|
+
|
132
|
+
`assign_attributes_from_page()` This will grab all the inputs that belong to a model instance and assign them to the attributes property as a javascript object.
|
133
|
+
|
134
|
+
`remove_errors_from_page()` This function will remove all errors from the page belonging to the model instance.
|
135
|
+
|
136
|
+
#####Base functions:
|
137
|
+
|
138
|
+
`set(attr_name, val)` Assigns the value to the model instance attributes object using the attribute name as a key.
|
139
|
+
|
140
|
+
```coffeescript
|
141
|
+
li = new App.LineItem()
|
142
|
+
li.set("foo", "bar")
|
143
|
+
li.attributes # => Object {uuid: "some-uuid", foo: "bar"}
|
144
|
+
```
|
145
|
+
|
146
|
+
`get(attr_name)` Retrieves the value from the model instance attributes object using the attribute name.
|
147
|
+
|
148
|
+
```coffeescript
|
149
|
+
li = new App.LineItem({foo: "bar"})
|
150
|
+
li.get("foo") # => "bar"
|
151
|
+
```
|
152
|
+
|
153
|
+
`save()` ajax post request to the route specified in the model with the data from the model instance attributes object to either post or patch depending on the presence of the id.
|
154
|
+
|
155
|
+
```coffeescript
|
156
|
+
li = new App.LineItem({foo: "bar"})
|
157
|
+
li.save() # => sends request to POST or PATCH depending on presince of id
|
158
|
+
```
|
159
|
+
|
160
|
+
```
|
161
|
+
# Server log
|
162
|
+
Started POST "/line_items.json" for 127.0.0.1 at 2014-12-14 02:35:32 -0800
|
163
|
+
Processing by LineItemsController#create as JSON
|
164
|
+
Parameters: {"line_item"=>{"uuid"=>"354f1fb8-a80a-449d-2320-e316bb02390c", "foo"=>"bar"}}
|
165
|
+
```
|
166
|
+
|
167
|
+
`destroy()` ajax delete request to the route specified in the model.
|
168
|
+
|
169
|
+
```coffeescript
|
170
|
+
li = new App.LineItem({id: 1})
|
171
|
+
li.destroy() # => Removes element from the page and sends delete request if id present
|
172
|
+
```
|
173
|
+
|
174
|
+
```
|
175
|
+
# Server log
|
176
|
+
Started DELETE "/line_items/1.json" for 127.0.0.1 at 2014-12-14 02:41:39 -0800
|
177
|
+
Processing by LineItemsController#destroy as JSON
|
178
|
+
Parameters: {"id"=>"1"}
|
179
|
+
```
|
180
|
+
|
181
|
+
`assign_attributes(attrs)` Adds the attrs to the attributes object for the model instance.
|
182
|
+
|
183
|
+
```coffeescript
|
184
|
+
li = new App.LineItem({foo: "bar"})
|
185
|
+
li.assign_attributes({baz: qux, quux: "corge"})
|
186
|
+
li.attributes # => Object {uuid: "some-uuid", foo: "bar", baz: "qux", quux: "corge"}
|
187
|
+
```
|
188
|
+
|
189
|
+
#####Base Overridable Hooks:
|
190
|
+
These hooks have defalut functionality but you can override them in the model to do custom behavior.
|
191
|
+
|
192
|
+
`after_save`
|
193
|
+
|
194
|
+
`after_save_error`
|
195
|
+
|
196
|
+
`after_destroy`
|
197
|
+
|
198
|
+
`after_destroy_error`
|
199
|
+
|
200
|
+
Here is an example where you are removing relevant errors from the page after deleting a line item.
|
201
|
+
|
202
|
+
```coffeescript
|
203
|
+
class App.LineItem extends App.Base
|
204
|
+
|
205
|
+
@route = "/line_items"
|
206
|
+
@set_class_name("LineItem")
|
207
|
+
|
208
|
+
# kindred override hook
|
209
|
+
after_destroy: (data, textStatus, xhr) ->
|
210
|
+
$("[data-error][data-k-uuid='" + @uuid + "']").parent().parent().remove()
|
211
|
+
```
|
212
|
+
|
213
|
+
####Class Functions
|
214
|
+
|
215
|
+
`set_template()` On page load, use this function to set the template for the model.
|
216
|
+
(i.e. `App.LineItem.set_template App.Template.template_info["line_item"]`)
|
217
|
+
|
218
|
+
`set_class_name()` Sets the class name for the model as well as dash_name and snake_name.
|
219
|
+
|
220
|
+
`collection_from_page()` Retrieves a collection of model objects from the page.
|
221
|
+
|
222
|
+
`save_all(opts)` Collects all the objects of this class from the page ajax posts the json to the save_all action.
|
223
|
+
|
224
|
+
###Listeners
|
225
|
+
Listeners in kindred should be namespaces and set in classes.
|
226
|
+
|
227
|
+
Below is an example of a listener that will both send a delete request to the server and remove the element from the page.
|
228
|
+
|
229
|
+
```coffeescript
|
230
|
+
# app/assets/javascripts/listeners/invoice_listeners.coffee
|
231
|
+
class App.InvoiceListeners extends App.Listener
|
232
|
+
|
233
|
+
@set: ->
|
234
|
+
$("#line-item-table").on "click.Listeners.LineItem.delete", ".delete", (evt) ->
|
235
|
+
li = new App.LineItem({id: $(@).data("id"), uuid: $(@).data("k-uuid")})
|
236
|
+
li.destroy()
|
237
|
+
$(@).parent().parent().remove()
|
238
|
+
```
|
239
|
+
|
240
|
+
A bonus for namespacing the listener is that you can see all the listeners that kindred has registerd using the `App.Listeners` class.
|
241
|
+
|
242
|
+
`App.Listeners` will return an object which contains all the registered listeners and information about each listener.
|
243
|
+
|
244
|
+
### Error Logging
|
245
|
+
|
246
|
+
All jquery element not found errors are logged to `App.Logger`
|
247
|
+
|
248
|
+
`App.Logger.errors` will return an array of errors with information including stack traces.
|
249
|
+
|
250
|
+
### Naming and Directory Structure
|
251
|
+
Although this is really up to the individual developer, Kindred should really be set up with a similar naming and directory stucture as rails.
|
252
|
+
|
253
|
+
If you are adding code that is controller and action specific, then add a directory called controllers in your `app/assets/javascripts` directory. If your controllers are namespaced then namespace them just like you do in your rails controllers. Here is an example of a namespaced coffee class:
|
254
|
+
|
255
|
+
```coffeescript
|
256
|
+
# app/assets/javascripts/controllers/admin/special/orders_controller.coffee
|
257
|
+
@Admin ||= {};
|
258
|
+
@Admin.Special ||= {};
|
259
|
+
|
260
|
+
class @Admin.Special.OrdersController
|
261
|
+
|
262
|
+
@index: (args) ->
|
263
|
+
alert("Do some js stuff here...")
|
264
|
+
```
|
265
|
+
|
266
|
+
Put models in `app/assets/javascripts/models`
|
267
|
+
|
268
|
+
```coffeescript
|
269
|
+
# app/assets/javascripts/models/some_model.coffee
|
270
|
+
class App.SomeModel
|
271
|
+
|
272
|
+
@route = "/some_models"
|
273
|
+
@set_class_name("SomeModel")
|
274
|
+
```
|
275
|
+
|
276
|
+
Make note of the ||=. This is to make sure that you don't overwrite the js object if it already exists.
|
277
|
+
|
278
|
+
Use this same naming and directory structure for all your js. If you are creating service objects then put them in `app/assets/javascripts/services`
|
279
|
+
|
280
|
+
Remember to add your paths to the manifest so sprockets can load them:
|
281
|
+
|
282
|
+
```
|
283
|
+
//= require_tree ./controllers
|
284
|
+
//= require_tree ./services
|
285
|
+
```
|
286
|
+
|
287
|
+
Or require them explicitly:
|
288
|
+
|
289
|
+
`//= require controllers/admin/special/orders_controller`
|
290
|
+
|
291
|
+
## Donating
|
292
|
+
Support this project and [others by mfpiccolo][gittip-mfpiccolo] via [gittip][gittip-mfpiccolo].
|
293
|
+
|
294
|
+
[gittip-mfpiccolo]: https://www.gittip.com/mfpiccolo/
|
295
|
+
|
296
|
+
## Copyright
|
297
|
+
|
298
|
+
Copyright (c) 2014 Mike Piccolo
|
299
|
+
|
300
|
+
See [LICENSE.txt](LICENSE.txt) for details.
|
301
|
+
|
302
|
+
## Contributing
|
303
|
+
|
304
|
+
1. Fork it ( http://github.com/mfpiccolo/kindred/fork )
|
305
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
306
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
307
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
308
|
+
5. Create new Pull Request
|
309
|
+
|
310
|
+
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/e1a155a07163d56ca0c4f246c7aa8766 "githalytics.com")](http://githalytics.com/mfpiccolo/kindred)
|
311
|
+
|
312
|
+
[license]: https://github.com/mfpiccolo/kindred/MIT-LICENSE
|
313
|
+
[homepage]: http://mfpiccolo.github.io/kindred
|
314
|
+
[documentation]: http://rdoc.info/github/mfpiccolo/kindred/frames
|
315
|
+
[issues]: https://github.com/mfpiccolo/kindred/issues
|
316
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
window.App = {}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
//= require app
|
2
|
+
//= require ./utilities/binder
|
3
|
+
//= require ./utilities/uuid
|
4
|
+
//= require ./utilities/virtual_class
|
5
|
+
//= require ./utilities/logger
|
6
|
+
//= require ./utilities/listener
|
7
|
+
//= require ./utilities/template
|
8
|
+
//= require ./utilities/stack_trace
|
9
|
+
//= require ./models/setup
|
10
|
+
//= require ./models/active_page
|
11
|
+
//= require ./models/base
|
12
|
+
|
13
|
+
|
@@ -0,0 +1,97 @@
|
|
1
|
+
class App.ActivePage
|
2
|
+
|
3
|
+
@collection_from_page: ->
|
4
|
+
indices = $("[data-kindred-model]").find("[data-k-uuid][data-class='#{@snake_name}']")
|
5
|
+
uuids = []
|
6
|
+
|
7
|
+
indices.map (i, tag) ->
|
8
|
+
uuids.push($(tag).data("k-uuid"))
|
9
|
+
|
10
|
+
# map args for JS array seem to differ from jQuery array above
|
11
|
+
collection_attrs = uuids.map (uuid, i) =>
|
12
|
+
new @({uuid: uuid}).assign_attributes_from_page().attributes
|
13
|
+
|
14
|
+
append_to_page: ->
|
15
|
+
$template = $(@template)
|
16
|
+
$.each @attributes, (key, value) =>
|
17
|
+
input = $template.find("input[data-attr='" + key + "']")
|
18
|
+
if input.length
|
19
|
+
if input.is(':checkbox')
|
20
|
+
input.prop('checked', value)
|
21
|
+
else
|
22
|
+
input.val(value)
|
23
|
+
|
24
|
+
select = $template.find("select[data-attr='" + key + "']")
|
25
|
+
if select.length
|
26
|
+
select.val(value)
|
27
|
+
|
28
|
+
span = $template.find("span[data-attr='" + key + "']")
|
29
|
+
if span.length
|
30
|
+
span.html(value)
|
31
|
+
|
32
|
+
@_append_data_model_to_page()
|
33
|
+
|
34
|
+
$("[data-target][data-target-uuid='" + @target_uuid + "']").append($template)
|
35
|
+
|
36
|
+
error_tag = $("[data-error][data-k-uuid='" + @uuid + "']")
|
37
|
+
error_tag.hide()
|
38
|
+
|
39
|
+
update_vals_on_page: ->
|
40
|
+
$.each @attributes, (attr, val) =>
|
41
|
+
$("[data-k-uuid='" + @uuid + "'][data-attr='" + attr + "']").val(val)
|
42
|
+
|
43
|
+
dirty_from_page: ->
|
44
|
+
dirty = []
|
45
|
+
$.each $("input[data-k-uuid='" + @uuid + "'], select[data-k-uuid='" + @uuid + "']"), (i, input) =>
|
46
|
+
$input = $(input)
|
47
|
+
|
48
|
+
dirty_object = {}
|
49
|
+
attr = $input.data("attr")
|
50
|
+
|
51
|
+
if @_input_dirty($input)
|
52
|
+
dirty.push(dirty_object[attr] = [$input.data("val").toString(), $input.val().toString()])
|
53
|
+
|
54
|
+
if dirty.length
|
55
|
+
true
|
56
|
+
else
|
57
|
+
false
|
58
|
+
|
59
|
+
assign_attributes_from_page: ->
|
60
|
+
$("input[data-k-uuid='" + @uuid + "']").each (i, input) =>
|
61
|
+
$input = $(input)
|
62
|
+
|
63
|
+
if $input.is(':checkbox')
|
64
|
+
@set $input.data("attr"), $input.prop('checked')
|
65
|
+
else
|
66
|
+
@set $input.data("attr"), $input.val()
|
67
|
+
|
68
|
+
model_data = $("[data-kindred-model]").find("[data-k-uuid='" + @uuid + "']")
|
69
|
+
if !isNaN(parseFloat(model_data.data("id"))) && isFinite(model_data.data("id"))
|
70
|
+
@id = model_data.data("id")
|
71
|
+
|
72
|
+
$("select[data-k-uuid='" + @uuid + "']").each (i, select) =>
|
73
|
+
@set $(select).data("attr"), $(select).val()
|
74
|
+
|
75
|
+
@
|
76
|
+
|
77
|
+
remove_errors_from_page: ->
|
78
|
+
$("[data-error][data-k-uuid='" + @uuid + "']").each (i, elem) =>
|
79
|
+
$(elem).remove()
|
80
|
+
|
81
|
+
_update_data_vals_on_page: ->
|
82
|
+
model_data = $("[data-kindred-model]").find("[data-k-uuid='" + @uuid + "']")
|
83
|
+
model_data.data("id", @id)
|
84
|
+
$.each @attributes, (attr, val) =>
|
85
|
+
$("[data-k-uuid='" + @uuid + "'][data-attr='" + attr + "']").data("val", val)
|
86
|
+
|
87
|
+
_append_data_model_to_page: ->
|
88
|
+
model_div = "<div data-k-uuid=" + @uuid + " data-id=" + @id + " data-class=" + @snake_name + "></div>"
|
89
|
+
$("[data-kindred-model]").append(model_div)
|
90
|
+
|
91
|
+
_input_dirty: (input) ->
|
92
|
+
if input.is("select") && input.data("val").length == 0
|
93
|
+
false
|
94
|
+
else if input.is(":checkbox")
|
95
|
+
!(input.data("val").toString() == input.prop("checked").toString())
|
96
|
+
else
|
97
|
+
!(input.data("val").toString() == input.val().toString())
|
@@ -0,0 +1,153 @@
|
|
1
|
+
class App.Base extends App.VirtualClass App.ActivePage, App.Setup
|
2
|
+
|
3
|
+
@set_template: (template) ->
|
4
|
+
@template = template
|
5
|
+
|
6
|
+
@save_all: (opts) ->
|
7
|
+
data = {}
|
8
|
+
# TODO fix the naive inflection
|
9
|
+
collection = @collection_from_page(@snake_name)
|
10
|
+
added_attrs = []
|
11
|
+
|
12
|
+
$.each collection, (i, attrs) ->
|
13
|
+
added_attrs.push($.extend attrs, opts.add_data_to_each)
|
14
|
+
|
15
|
+
data[@snake_name + "s"] = added_attrs
|
16
|
+
|
17
|
+
url_match = new RegExp(/{{([^{}]+)}}/g)
|
18
|
+
@route = @route.replace(url_match, (match, p1) =>
|
19
|
+
attribute = match.slice(2, - 2)
|
20
|
+
opts.add_data_to_each[attribute]
|
21
|
+
)
|
22
|
+
|
23
|
+
path = @route + "/save_all.json"
|
24
|
+
method = 'PUT'
|
25
|
+
|
26
|
+
$.ajax
|
27
|
+
type: method
|
28
|
+
url: App.BaseUrl + "/" + path
|
29
|
+
data: data
|
30
|
+
|
31
|
+
success: (data, textStatus, xhr) =>
|
32
|
+
@after_save_all(data, textStatus, xhr)
|
33
|
+
error: (xhr) =>
|
34
|
+
@after_save_all_error(xhr)
|
35
|
+
|
36
|
+
# The attribute setter publish changes using the DataBinder PubSub
|
37
|
+
set: (attr_name, val) ->
|
38
|
+
@attributes[attr_name] = val
|
39
|
+
# @binder.trigger @id + ":change", [
|
40
|
+
# attr_name
|
41
|
+
# val
|
42
|
+
# @
|
43
|
+
# ]
|
44
|
+
|
45
|
+
get: (attr_name) ->
|
46
|
+
@attributes[attr_name]
|
47
|
+
|
48
|
+
remove: (attr_name) ->
|
49
|
+
delete @attributes[attr_name]
|
50
|
+
|
51
|
+
save: ->
|
52
|
+
if !isNaN(parseFloat(@id)) && isFinite(@id)
|
53
|
+
path = @route + "/" + @id + ".json"
|
54
|
+
method = 'PUT'
|
55
|
+
else
|
56
|
+
path = @route + ".json"
|
57
|
+
method = "POST"
|
58
|
+
|
59
|
+
params = {}
|
60
|
+
params[@snake_name] = @attributes
|
61
|
+
|
62
|
+
response = $.ajax
|
63
|
+
type: method
|
64
|
+
url: App.BaseUrl + "/" + path
|
65
|
+
dataType: "json"
|
66
|
+
data: params
|
67
|
+
global: false
|
68
|
+
async: false
|
69
|
+
success: (data, textStatus, xhr) =>
|
70
|
+
@after_save(data, textStatus, xhr)
|
71
|
+
error: (xhr) =>
|
72
|
+
@after_save_error(xhr)
|
73
|
+
|
74
|
+
destroy: ->
|
75
|
+
@route ||= @snake_name + "s"
|
76
|
+
path = @route + "/" + @id + ".json"
|
77
|
+
method = "DELETE"
|
78
|
+
|
79
|
+
if !isNaN(parseFloat(@id)) && isFinite(@id)
|
80
|
+
$.ajax
|
81
|
+
type: method
|
82
|
+
url: App.BaseUrl + "/" + path
|
83
|
+
dataType: "json"
|
84
|
+
global: false
|
85
|
+
async: false
|
86
|
+
success: (data, textStatus, xhr) =>
|
87
|
+
@after_destroy(data, textStatus, xhr)
|
88
|
+
error: (xhr) =>
|
89
|
+
@after_destroy_error(xhr)
|
90
|
+
else
|
91
|
+
@after_destroy()
|
92
|
+
|
93
|
+
assign_attributes: (attrs) ->
|
94
|
+
$.each attrs, (attr, val) =>
|
95
|
+
if attr == "id" && !isNaN(parseFloat(val)) && isFinite(val)
|
96
|
+
@id = val
|
97
|
+
@set(attr, val)
|
98
|
+
|
99
|
+
#overridable hook
|
100
|
+
after_save: (data, textStatus, xhr) ->
|
101
|
+
@assign_attributes(data)
|
102
|
+
@_clear_errors()
|
103
|
+
@_update_data_vals_on_page()
|
104
|
+
@_setup_interpolated_vars()
|
105
|
+
|
106
|
+
#overridable hook
|
107
|
+
after_save_error: (xhr) ->
|
108
|
+
errors = JSON.parse(xhr.responseText)
|
109
|
+
@_handle_errors(errors)
|
110
|
+
|
111
|
+
#overridable hook
|
112
|
+
after_destroy: (data, textStatus, xhr) ->
|
113
|
+
@remove_errors_from_page()
|
114
|
+
|
115
|
+
#overridable hook
|
116
|
+
after_destroy_error: (xhr) ->
|
117
|
+
|
118
|
+
#overridable hook
|
119
|
+
@after_save_all: (data, textStatus, xhr) ->
|
120
|
+
$(data).each (i, response_object) =>
|
121
|
+
attrs = response_object[@snake_name]
|
122
|
+
model = new App[@class_name]({uuid: attrs["uuid"]})
|
123
|
+
model.assign_attributes(attrs)
|
124
|
+
model._clear_errors()
|
125
|
+
model._update_data_vals_on_page()
|
126
|
+
|
127
|
+
#overridable hook
|
128
|
+
@after_save_all_error: (xhr) ->
|
129
|
+
data = JSON.parse(xhr.responseText)
|
130
|
+
$(data).each (i, response_object) =>
|
131
|
+
unless $.isEmptyObject(response_object["errors"])
|
132
|
+
uuid = response_object[@snake_name].uuid
|
133
|
+
model = new App[@class_name](response_object[@snake_name])
|
134
|
+
model.assign_attributes_from_page()
|
135
|
+
model._handle_errors(response_object["errors"])
|
136
|
+
|
137
|
+
_handle_errors: (errors_obj, uuid) ->
|
138
|
+
hideable_error_inputs = $(Object.keys(@attributes)).not(Object.keys(errors_obj)).get()
|
139
|
+
$.each hideable_error_inputs, (i, attr) =>
|
140
|
+
$("[data-error][data-attr='" + attr + "'][data-k-uuid='" + @uuid + "']").hide()
|
141
|
+
|
142
|
+
$.each errors_obj, (attr, messages) =>
|
143
|
+
error_tag = $("[data-error][data-attr='" + attr + "'][data-k-uuid='" + @uuid + "']")
|
144
|
+
error_tag.html("")
|
145
|
+
|
146
|
+
$.each messages, (i, message) ->
|
147
|
+
error_tag.append("<span>" + message + "</span><br>")
|
148
|
+
|
149
|
+
error_tag.show()
|
150
|
+
|
151
|
+
_clear_errors: () ->
|
152
|
+
$.each @attributes, (attr, val) =>
|
153
|
+
$("[data-error][data-attr='" + attr + "'][data-k-uuid='" + @uuid + "']").hide()
|