js-model-rails 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/assets/javascripts/js-model/js-model.js +51 -21
- data/lib/js-model/version.rb +1 -1
- metadata +6 -5
@@ -1,16 +1,16 @@
|
|
1
|
-
/* js-model JavaScript library, version 0.
|
2
|
-
* (c) 2010-
|
1
|
+
/* js-model JavaScript library, version 0.11.0
|
2
|
+
* (c) 2010-2012 Ben Pickles
|
3
3
|
*
|
4
4
|
* Released under MIT license.
|
5
5
|
*/
|
6
6
|
var Model = function(name, func) {
|
7
7
|
// The model constructor.
|
8
8
|
var model = function(attributes) {
|
9
|
-
this.attributes =
|
9
|
+
this.attributes = Model.Utils.extend({}, attributes)
|
10
10
|
this.changes = {};
|
11
11
|
this.errors = new Model.Errors(this);
|
12
12
|
this.uid = [name, Model.UID.generate()].join("-")
|
13
|
-
if (
|
13
|
+
if (Model.Utils.isFunction(this.initialize)) this.initialize()
|
14
14
|
};
|
15
15
|
|
16
16
|
// Use module functionality to extend itself onto the constructor. Meta!
|
@@ -26,7 +26,7 @@ var Model = function(name, func) {
|
|
26
26
|
model.prototype = new Model.Base
|
27
27
|
model.prototype.constructor = model
|
28
28
|
|
29
|
-
if (
|
29
|
+
if (Model.Utils.isFunction(func)) func.call(model, model, model.prototype)
|
30
30
|
|
31
31
|
return model;
|
32
32
|
};
|
@@ -76,7 +76,7 @@ Model.ClassMethods = {
|
|
76
76
|
add: function(model) {
|
77
77
|
var id = model.id()
|
78
78
|
|
79
|
-
if (
|
79
|
+
if (Model.Utils.inArray(this.collection, model) === -1 && !(id && this.find(id))) {
|
80
80
|
this.collection.push(model)
|
81
81
|
this.trigger("add", [model])
|
82
82
|
}
|
@@ -90,7 +90,7 @@ Model.ClassMethods = {
|
|
90
90
|
|
91
91
|
// Convenience method to allow a simple method of chaining class methods.
|
92
92
|
chain: function(collection) {
|
93
|
-
return
|
93
|
+
return Model.Utils.extend({}, this, { collection: collection || [] })
|
94
94
|
},
|
95
95
|
|
96
96
|
count: function() {
|
@@ -223,7 +223,7 @@ Model.ClassMethods = {
|
|
223
223
|
},
|
224
224
|
|
225
225
|
sortBy: function(attribute_or_func) {
|
226
|
-
var is_func =
|
226
|
+
var is_func = Model.Utils.isFunction(attribute_or_func)
|
227
227
|
var extract = function(model) {
|
228
228
|
return attribute_or_func.call(model)
|
229
229
|
}
|
@@ -242,10 +242,10 @@ Model.ClassMethods = {
|
|
242
242
|
})
|
243
243
|
},
|
244
244
|
|
245
|
-
use: function(
|
245
|
+
use: function(plugin) {
|
246
246
|
var args = Array.prototype.slice.call(arguments, 1)
|
247
247
|
args.unshift(this)
|
248
|
-
|
248
|
+
plugin.apply(this, args)
|
249
249
|
return this
|
250
250
|
}
|
251
251
|
};
|
@@ -299,7 +299,7 @@ Model.InstanceMethods = {
|
|
299
299
|
attr: function(name, value) {
|
300
300
|
if (arguments.length === 0) {
|
301
301
|
// Combined attributes/changes object.
|
302
|
-
return
|
302
|
+
return Model.Utils.extend({}, this.attributes, this.changes);
|
303
303
|
} else if (arguments.length === 2) {
|
304
304
|
// Don't write to attributes yet, store in changes for now.
|
305
305
|
if (this.attributes[name] === value) {
|
@@ -308,12 +308,14 @@ Model.InstanceMethods = {
|
|
308
308
|
} else {
|
309
309
|
this.changes[name] = value;
|
310
310
|
}
|
311
|
+
this.trigger("change:" + name, [this])
|
311
312
|
return this;
|
312
313
|
} else if (typeof name === "object") {
|
313
314
|
// Mass-assign attributes.
|
314
315
|
for (var key in name) {
|
315
316
|
this.attr(key, name[key]);
|
316
317
|
}
|
318
|
+
this.trigger("change", [this])
|
317
319
|
return this;
|
318
320
|
} else {
|
319
321
|
// Changes take precedent over attributes.
|
@@ -379,7 +381,7 @@ Model.InstanceMethods = {
|
|
379
381
|
},
|
380
382
|
|
381
383
|
merge: function(attributes) {
|
382
|
-
|
384
|
+
Model.Utils.extend(this.attributes, attributes);
|
383
385
|
return this;
|
384
386
|
},
|
385
387
|
|
@@ -447,14 +449,14 @@ Model.localStorage = function(klass) {
|
|
447
449
|
var addToIndex = function(uid) {
|
448
450
|
var uids = readIndex()
|
449
451
|
|
450
|
-
if (
|
452
|
+
if (Model.Utils.inArray(uids, uid) === -1) {
|
451
453
|
uids.push(uid)
|
452
454
|
writeIndex(uids)
|
453
455
|
}
|
454
456
|
}
|
455
457
|
var removeFromIndex = function(uid) {
|
456
458
|
var uids = readIndex()
|
457
|
-
var index =
|
459
|
+
var index = Model.Utils.inArray(uids, uid)
|
458
460
|
|
459
461
|
if (index > -1) {
|
460
462
|
uids.splice(index, 1)
|
@@ -489,7 +491,7 @@ Model.localStorage = function(klass) {
|
|
489
491
|
for (var i = 0, length = uids.length; i < length; i++) {
|
490
492
|
uid = uids[i]
|
491
493
|
|
492
|
-
if (
|
494
|
+
if (Model.Utils.inArray(existing_uids, uid) == -1) {
|
493
495
|
attributes = JSON.parse(localStorage[uid])
|
494
496
|
model = new klass(attributes)
|
495
497
|
model.uid = uid
|
@@ -513,12 +515,12 @@ Model.Log = function() {
|
|
513
515
|
|
514
516
|
Model.Module = {
|
515
517
|
extend: function(obj) {
|
516
|
-
|
518
|
+
Model.Utils.extend(this, obj)
|
517
519
|
return this
|
518
520
|
},
|
519
521
|
|
520
522
|
include: function(obj) {
|
521
|
-
|
523
|
+
Model.Utils.extend(this.prototype, obj)
|
522
524
|
return this
|
523
525
|
}
|
524
526
|
};
|
@@ -536,7 +538,7 @@ Model.REST = function(klass, resource, methods) {
|
|
536
538
|
return resource_param_names
|
537
539
|
})()
|
538
540
|
|
539
|
-
return
|
541
|
+
return Model.Utils.extend({
|
540
542
|
path: function(model) {
|
541
543
|
var path = resource;
|
542
544
|
jQuery.each(resource_param_names, function(i, param) {
|
@@ -572,7 +574,7 @@ Model.REST = function(klass, resource, methods) {
|
|
572
574
|
params = null;
|
573
575
|
}
|
574
576
|
if(jQuery.ajaxSettings.data){
|
575
|
-
params =
|
577
|
+
params = Model.Utils.extend({}, jQuery.ajaxSettings.data, params)
|
576
578
|
}
|
577
579
|
return JSON.stringify(params)
|
578
580
|
},
|
@@ -679,10 +681,38 @@ Model.UID = {
|
|
679
681
|
}
|
680
682
|
};
|
681
683
|
|
682
|
-
Model.
|
684
|
+
Model.Utils = {
|
685
|
+
extend: function(receiver) {
|
686
|
+
var objs = Array.prototype.slice.call(arguments, 1)
|
687
|
+
|
688
|
+
for (var i = 0, length = objs.length; i < length; i++) {
|
689
|
+
for (var property in objs[i]) {
|
690
|
+
receiver[property] = objs[i][property]
|
691
|
+
}
|
692
|
+
}
|
693
|
+
|
694
|
+
return receiver
|
695
|
+
},
|
696
|
+
|
697
|
+
inArray: function(array, obj) {
|
698
|
+
if (array.indexOf) return array.indexOf(obj)
|
699
|
+
|
700
|
+
for (var i = 0, length = array.length; i < length; i++) {
|
701
|
+
if (array[i] === obj) return i
|
702
|
+
}
|
703
|
+
|
704
|
+
return -1
|
705
|
+
},
|
706
|
+
|
707
|
+
isFunction: function(obj) {
|
708
|
+
return Object.prototype.toString.call(obj) === "[object Function]"
|
709
|
+
}
|
710
|
+
}
|
711
|
+
|
712
|
+
Model.VERSION = "0.11.0";
|
683
713
|
|
684
714
|
Model.Base = (function() {
|
685
715
|
function Base() {}
|
686
|
-
Base.prototype =
|
716
|
+
Base.prototype = Model.Utils.extend({}, Model.Callbacks, Model.InstanceMethods)
|
687
717
|
return Base
|
688
718
|
})();
|
data/lib/js-model/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js-model-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-03-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: railties
|
16
|
-
requirement: &
|
16
|
+
requirement: &70245243659460 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70245243659460
|
25
25
|
description: Work with models in your JavaScript in your Rails
|
26
26
|
email:
|
27
27
|
- spideryoung@gmail.com
|
@@ -59,8 +59,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
59
|
version: '0'
|
60
60
|
requirements: []
|
61
61
|
rubyforge_project: js-model-rails
|
62
|
-
rubygems_version: 1.8.
|
62
|
+
rubygems_version: 1.8.15
|
63
63
|
signing_key:
|
64
64
|
specification_version: 3
|
65
65
|
summary: Work with models in your JavaScript in your Rails
|
66
66
|
test_files: []
|
67
|
+
has_rdoc:
|