forked from jupiterjs/jquerymx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.js
More file actions
1011 lines (967 loc) · 28.8 KB
/
model.js
File metadata and controls
1011 lines (967 loc) · 28.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global OpenAjax: true */
steal.plugins('jquery/class', 'jquery/lang').then(function() {
//a cache for attribute capitalization ... slowest part of init.
var underscore = $.String.underscore,
classize = $.String.classize;
/**
* @tag core
* @download jquery/dist/jquery.model.js
* @test jquery/model/qunit.html
* @plugin jquery/model
*
* Models wrap an application's data layer. In large applications, a model is critical for:
*
* - Encapsulating services so controllers + views don't care where data comes from.
*
* - Providing helper functions that make manipulating and abstracting raw service data easier.
*
* This is done in two ways:
*
* - Requesting data from and interacting with services
*
* - Converting or wrapping raw service data into a more useful form.
*
*
* ## Basic Use
*
* The [jQuery.Model] class provides a basic skeleton to organize pieces of your application's data layer.
* First, consider doing Ajax <b>without</b> a model. In our imaginary app, you:
*
* - retrieve a list of tasks</li>
* - display the number of days remaining for each task
* - mark tasks as complete after users click them
*
* Let's see how that might look without a model:
*
* @codestart
* $.Controller.extend("MyApp.Controllers.Tasks",{onDocument: true},
* {
* // get tasks when the page is ready
* ready: function() {
* $.get('/tasks.json', this.callback('gotTasks'), 'json')
* },
* |*
* * assume json is an array like [{name: "trash", due_date: 1247111409283}, ...]
* *|
* gotTasks: function( json ) {
* for(var i =0; i < json.length; i++){
* var taskJson = json[i];
*
* //calculate time remaining
* var remaininTime = new Date() - new Date(taskJson.due_date);
*
* //append some html
* $("#tasks").append("<div class='task' taskid='"+taskJson.id+"'>"+
* "<label>"+taskJson.name+"</label>"+
* "Due Date = "+remaininTime+"</div>")
* }
* },
* // when a task is complete, get the id, make a request, remove it
* ".task click" : function( el ) {
* $.post('/task_complete',{id: el.attr('data-taskid')}, function(){
* el.remove();
* })
* }
* })
* @codeend
*
* This code might seem fine for right now, but what if:
*
* - The service changes?
* - Other parts of the app want to calculate <code>remaininTime</code>?
* - Other parts of the app want to get tasks?</li>
* - The same task is represented multiple palces on the page?
*
* The solution is of course a strong model layer. Lets look at what a
* a good model does for a controller before we learn how to make one:
*
* @codestart
* $.Controller.extend("MyApp.Controllers.Tasks",{onDocument: true},
* {
* load: function() {
* Task.findAll({},this.callback('list'))
* },
* list: function( tasks ) {
* $("#tasks").html(this.view(tasks))
* },
* ".task click" : function( el ) {
* el.models()[0].complete(function(){
* el.remove();
* });
* }
* })
* @codeend
*
* In views/tasks/list.ejs
*
* @codestart html
* <% for(var i =0; i < tasks.length; i++){ %>
* <div class='task <%= tasks[i].<b>identity</b>() %>'>
* <label><%= tasks[i].name %></label>
* <%= tasks[i].<b>timeRemaining</b>() %>
* </div>
* <% } %>
* @codeend
*
* Isn't that better! Granted, some of the improvement comes because we used a view, but we've
* also made our controller completely understandable. Now lets take a look at the model:
*
* @codestart
* $.Model.extend("Task",
* {
* findAll: function( params,success ) {
* $.get("/tasks.json", params, this.callback(["wrapMany",success]),"json");
* }
* },
* {
* timeRemaining: function() {
* return new Date() - new Date(this.due_date)
* },
* complete: function( success ) {
* $.get("/task_complete", {id: this.id }, success,"json");
* }
* })
* @codeend
*
* There, much better! Now you have a single place where you can organize Ajax functionality and
* wrap the data that it returned. Lets go through each bolded item in the controller and view.<br/>
*
* ### Task.findAll
*
* The findAll function requests data from "/tasks.json". When the data is returned, it it is run through
* the "wrapMany" function before being passed to the success callback.<br/>
* If you don't understand how the callback works, you might want to check out
* [jQuery.Model.static.wrapMany wrapMany] and [jQuery.Class.static.callback callback].
*
* ### el.models
*
* [jQuery.fn.models models] is a jQuery helper that returns model instances. It uses
* the jQuery's elements' shortNames to find matching model instances. For example:
*
* @codestart html
* <div class='task task_5'> ... </div>
* @codeend
*
* It knows to return a task with id = 5.
*
* ### complete
*
* This should be pretty obvious.
*
* ### identity
*
* [jQuery.Model.prototype.identity Identity] returns a unique identifier that [jQuery.fn.models] can use
* to retrieve your model instance.
*
* ### timeRemaining
*
* timeRemaining is a good example of wrapping your model's raw data with more useful functionality.
* ## Validations
*
* You can validate your model's attributes with another plugin. See [validation].
*/
jQuery.Class.extend("jQuery.Model",
/**
* @Static
*/
{
setup: function( superClass ) {
//we do not inherit attributes (or associations)
if (!this.attributes || superClass.attributes === this.attributes ) {
this.attributes = {};
}
if (!this.associations || superClass.associations === this.associations ) {
this.associations = {};
}
if (!this.validations || superClass.validations === this.validations ) {
this.validations = {};
}
//add missing converters
if ( superClass.convert != this.convert ) {
this.convert = $.extend(superClass.convert, this.convert);
}
this._fullName = underscore(this.fullName.replace(/\./g, "_"));
if ( this.fullName.substr(0, 7) == "jQuery." ) {
return;
}
//add this to the collection of models
jQuery.Model.models[this._fullName] = this;
if ( this.listType ) {
this.list = new this.listType([]);
}
},
/**
* @attribute attributes
* Attributes contains a list of properties and their types
* for this model. You can use this in conjunction with
* [jQuery.Model.static.convert] to provide automatic
* [jquery.model.typeconversion type conversion].
*
* The following converts dueDates to JavaScript dates:
*
* @codestart
* $.Model.extend("Contact",{
* attributes : {
* birthday : 'date'
* },
* convert : {
* date : function(raw){
* if(typeof raw == 'string'){
* var matches = raw.match(/(\d+)-(\d+)-(\d+)/)
* return new Date( matches[1],
* (+matches[2])-1,
* matches[3] )
* }else if(raw instanceof Date){
* return raw;
* }
* }
* }
* },{})
* @codeend
*/
attributes: {},
/**
* @attribute defaults
* An object of default values to be set on all instances. This
* is useful if you want some value to be present when new instances are created.
*
* @codestart
* $.Model.extend("Recipe",{
* defaults : {
* createdAt : new Date();
* }
* },{})
*
* var recipe = new Recipe();
*
* recipe.createdAt //-> date
*
* @codeend
*/
defaults: {},
/**
* Wrap is used to create a new instance from data returned from the server.
* It is very similar to doing <code> new Model(attributes) </code>
* except that wrap will check if the data passed has an
*
* - attributes,
* - data, or
* - <i>singularName</i>
*
* property. If it does, it will use that objects attributes.
*
* Wrap is really a convience method for servers that don't return just attributes.
*
* @param {Object} attributes
* @return {Model} an instance of the model
*/
wrap: function( attributes ) {
if (!attributes ) {
return null;
}
return new this(
// checks for properties in an object (like rails 2.0 gives);
attributes[this.singularName] || attributes.data || attributes.attributes || attributes);
},
/**
* Takes raw data from the server, and returns an array of model instances.
* Each item in the raw array becomes an instance of a model class.
*
* @codestart
* $.Model.extend("Recipe",{
* helper : function(){
* return i*i;
* }
* })
*
* var recipes = Recipe.wrapMany([{id: 1},{id: 2}])
* recipes[0].helper() //-> 1
* @codeend
*
* If an array is not passed to wrapMany, it will look in the object's .data
* property.
*
* For example:
*
* @codestart
* var recipes = Recipe.wrapMany({data: [{id: 1},{id: 2}]})
* recipes[0].helper() //-> 1
* @codeend
*
* @param {Array} instancesRawData an array of raw name - value pairs.
* @return {Array} a JavaScript array of instances or a [jQuery.Model.List list] of instances
* if the model list plugin has been included.
*/
wrapMany: function( instancesRawData ) {
if (!instancesRawData ) {
return null;
}
var listType = this.List || $.Model.List || Array,
res = new listType(),
arr = $.isArray(instancesRawData),
raw = arr ? instancesRawData : instancesRawData.data,
length = raw.length,
i = 0;
res._use_call = true; //so we don't call next function with all of these
for (; i < length; i++ ) {
res.push(this.wrap(raw[i]));
}
if (!arr ) { //push other stuff onto array
for ( var prop in instancesRawData ) {
if ( prop !== 'data' ) {
res[prop] = instancesRawData[prop];
}
}
}
return res;
},
/**
* The name of the id field. Defaults to 'id'. Change this if it is something different.
*
* For example, it's common in .NET to use Id. Your model might look like:
*
* @codestart
* $.Model.extend("Friends",{
* id: "Id"
* },{});
* @codeend
*/
id: 'id',
//if null, maybe treat as an array?
/**
* Adds an attribute to the list of attributes for this class.
* @hide
* @param {String} property
* @param {String} type
*/
addAttr: function( property, type ) {
var stub;
if ( this.associations[property] ) {
return;
}
stub = this.attributes[property] || (this.attributes[property] = type);
return type;
},
// a collection of all models
models: {},
/**
* If OpenAjax is available,
* publishes to OpenAjax.hub. Always adds the shortName.event.
*
* @codestart
* // publishes contact.completed
* Namespace.Contact.publish("completed",contact);
* @codeend
*
* @param {String} event The event name to publish
* @param {Object} data The data to publish
*/
publish: function( event, data ) {
//@steal-remove-start
steal.dev.log("Model.js - publishing " + underscore(this.shortName) + "." + event);
//@steal-remove-end
if ( window.OpenAjax ) {
OpenAjax.hub.publish(underscore(this.shortName) + "." + event, data);
}
},
/**
* @hide
* Guesses the type of an object. This is what sets the type if not provided in
* [jQuery.Model.static.attributes].
* @param {Object} object the object you want to test.
* @return {String} one of string, object, date, array, boolean, number, function
*/
guessType: function( object ) {
if ( typeof object != 'string' ) {
if ( object === null ) {
return typeof object;
}
if ( object.constructor == Date ) {
return 'date';
}
if ( $.isArray(object) ) {
return 'array';
}
return typeof object;
}
if ( object === "" ) {
return 'string';
}
//check if true or false
if ( object == 'true' || object == 'false' ) {
return 'boolean';
}
if (!isNaN(object) && isFinite(+object) ) {
return 'number';
}
return typeof object;
},
/**
* @attribute convert
* @type Object
* An object of name-function pairs that are used to convert attributes.
* Check out [jQuery.Model.static.attributes] or
* [jquery.model.typeconversion type conversion]
* for examples.
*/
convert: {
"date": function( str ) {
return typeof str === "string" ? (isNaN(Date.parse(str)) ? null : Date.parse(str)) : str;
},
"number": function( val ) {
return parseFloat(val);
},
"boolean": function( val ) {
return Boolean(val);
}
},
/**
* Implement this function!
* Create is called by save to create a new instance. If you want to be able to call save on an instance
* you have to implement create.
*/
create: function( attrs, success, error ) {
throw "Model: Implement Create";
},
/**
* Implement this function!
* Update is called by save to update an instance. If you want to be able to call save on an instance
* you have to implement update.
*/
update: function( id, attrs, success, error ) {
throw "Model: Implement " + this.fullName + "'s \"update\"!";
},
/**
* Implement this function!
* Destroy is called by destroy to remove an instance. If you want to be able to call destroy on an instance
* you have to implement update.
* @param {String|Number} id the id of the instance you want destroyed
*/
destroy: function( id, success, error ) {
throw "Model: Implement " + this.fullName + "'s \"destroy\"!";
},
/**
* Implement this function!
* @param {Object} params
* @param {Function} success
* @param {Function} error
*/
findAll: function( params, success, error ) {
},
/**
* Implement this function!
* @param {Object} params
* @param {Function} success
* @param {Function} error
*/
findOne: function( params, success, error ) {
}
},
/**
* @Prototype
*/
{
/**
* Setup is called when a new model instance is created.
* It adds default attributes, then whatever attributes
* are passed to the class.
* Setup should never be called directly.
*
* @codestart
* $.Model.extend("Recipe")
* var recipe = new Recipe({foo: "bar"});
* recipe.foo //-> "bar"
* recipe.attr("foo") //-> "bar"
* @codeend
*
* @param {Object} attributes a hash of attributes
*/
setup: function( attributes ) {
var stub;
// so we know not to fire events
this._initializing = true;
stub = this.Class.defaults && this.attrs(this.Class.defaults);
this.attrs(attributes);
delete this._initializing;
},
/**
* Sets the attributes on this instance and calls save.
* The instance needs to have an id. It will use
* the instance class's [jQuery.Model.static.update update]
* method.
*
* @codestart
* recipe.update({name: "chicken"}, success, error);
* @codeend
*
* If OpenAjax.hub is available, the model will also
* publish a "<i>modelName</i>.updated" message with
* the updated instance.
*
* @param {Object} attrs the model's attributes
* @param {Function} success called if a successful update
* @param {Function} error called if there's an error
*/
update: function( attrs, success, error ) {
this.attrs(attrs);
return this.save(success, error); //on success, we should
},
/**
* Runs the validations on this model. You can
* also pass it an array of attributes to run only those attributes.
* It returns nothing if there are no errors, or an object
* of errors by attribute.
*
* To use validations, it's suggested you use the
* model/validations plugin.
*
* @codestart
* $.Model.extend("Task",{
* init : function(){
* this.validatePresenceOf("dueDate")
* }
* },{});
*
* var task = new Task(),
* errors = task.errors()
*
* errors.dueDate[0] //-> "can't be empty"
* @codeend
*/
errors: function( attrs ) {
if ( attrs ) {
attrs = $.isArray(attrs) ? attrs : $.makeArray(arguments);
}
var errors = {},
self = this,
addErrors = function( attr, funcs ) {
$.each(funcs, function( i, func ) {
var res = func.call(self);
if ( res ) {
if (!errors.hasOwnProperty(attr) ) {
errors[attr] = [];
}
errors[attr].push(res);
}
});
};
$.each(attrs || this.Class.validations || {}, function( attr, funcs ) {
if ( typeof attr == 'number' ) {
attr = funcs;
funcs = self.Class.validations[attr];
}
addErrors(attr, funcs || []);
});
for ( var attr in errors ) {
if ( errors.hasOwnProperty(attr) ) {
return errors;
}
}
return null;
},
/**
* Gets or sets an attribute on the model using setters and
* getters if available.
*
* @codestart
* $.Model.extend("Recipe")
* var recipe = new Recipe();
* recipe.attr("foo","bar")
* recipe.foo //-> "bar"
* recipe.attr("foo") //-> "bar"
* @codeend
*
* ## Setters
*
* If you add a set<i>AttributeName</i> method on your model,
* it will be used to set the value. The set method is called
* with the value and is expected to return the converted value.
*
* @codestart
* $.Model.extend("Recipe",{
* setCreatedAt : function(raw){
* return Date.parse(raw)
* }
* })
* var recipe = new Recipe();
* recipe.attr("createdAt","Dec 25, 1995")
* recipe.createAt //-> Date
* @codeend
*
* ## Asynchronous Setters
*
* Sometimes, you want to perform an ajax request when
* you set a property. You can do this with setters too.
*
* To do this, your setter should return undefined and
* call success with the converted value. For example:
*
* @codestart
* $.Model.extend("Recipe",{
* setTitle : function(title, success, error){
* $.post(
* "recipe/update/"+this.id+"/title",
* title,
* function(){
* success(title);
* },
* "json")
* }
* })
*
* recipe.attr("title","fish")
* @codeend
*
* ## Events
*
* When you use attr, it can also trigger events. This is
* covered in [jQuery.Model.prototype.bind].
*
* @param {String} attribute the attribute you want to set or get
* @param {String|Number|Boolean} [value] value the value you want to set.
* @param {Function} [success] an optional success callback.
* This gets called if the attribute was successful.
* @param {Function} [error] an optional success callback.
* The error function is called with validation errors.
*/
attr: function( attribute, value, success, error ) {
var cap = classize(attribute),
get = "get" + cap;
if ( value !== undefined ) {
this._setProperty(attribute, value, success, error, cap);
return this;
}
return this[get] ? this[get]() : this[attribute];
},
/**
* Binds to events on this model instance. Typically
* you'll bind to an attribute name. Handler will be called
* every time the attribute value changes. For example:
*
* @codestart
* $.Model.extend("School")
* var school = new School();
* school.bind("address", function(ev, address){
* alert('address changed to '+address);
* })
* school.attr("address","1124 Park St");
* @codeend
*
* You can also bind to attribute errors.
*
* @codestart
* $.Model.extend("School",{
* setName : function(name, success, error){
* if(!name){
* error("no name");
* }
* return error;
* }
* })
* var school = new School();
* school.bind("error.name", function(ev, mess){
* mess // -> "no name";
* })
* school.attr("name","");
* @codeend
*
* You can also bind to created, updated, and destroyed events.
*
* @param {String} eventType the name of the event.
* @param {Function} handler a function to call back when an event happens on this model.
* @return {model} the model instance for chaining
*/
bind: function( eventType, handler ) {
var wrapped = $(this);
wrapped.bind.apply(wrapped, arguments);
return this;
},
/**
* Unbinds an event handler from this instance.
* Read [jQuery.Model.prototype.bind] for
* more information.
* @param {String} eventType
* @param {Function} handler
*/
unbind: function( eventType, handler ) {
var wrapped = $(this);
wrapped.unbind.apply(wrapped, arguments);
return this;
},
/**
* Checks if there is a set_<i>property</i> value. If it returns true, lets it handle; otherwise
* saves it.
* @hide
* @param {Object} property
* @param {Object} value
*/
_setProperty: function( property, value, success, error, capitalized ) {
// the potential setter name
var setName = "set" + capitalized,
//the old value
old = this[property],
self = this,
errorCallback = function( errors ) {
var stub;
stub = error && error.call(self, errors);
$(self).triggerHandler("error." + property, errors);
};
// if the setter returns nothing, do not set
// we might want to indicate if this was set ok
if ( this[setName] && (value = this[setName](value, this.callback('_updateProperty', property, value, old, success, errorCallback), errorCallback)) === undefined ) {
return;
}
this._updateProperty(property, value, old, success, errorCallback);
},
/**
* Triggers events when a property has been updated
* @hide
* @param {Object} property
* @param {Object} value
* @param {Object} old
* @param {Object} success
*/
_updateProperty: function( property, value, old, success, errorCallback ) {
var Class = this.Class,
val, type = Class.attributes[property] || Class.addAttr(property, Class.guessType(value)),
//the converter
converter = Class.convert[type],
errors = null,
stub;
val = this[property] = (value === null ? //if the value is null or undefined
null : // it should be null
(converter ? converter.call(Class, value) : //convert it to something useful
value)); //just return it
//validate (only if not initializing, this is for performance)
if (!this._initializing ) {
errors = this.errors(property);
}
if ( errors ) {
errorCallback(errors);
} else {
if ( old !== val && !this._initializing ) {
$(this).triggerHandler(property, val);
}
stub = success && success(this);
}
//if this class has a global list, add / remove from the list.
if ( property == Class.id && val !== null && Class.list ) {
// if we didn't have an old id, add ourselves
if (!old ) {
Class.list.push(this);
} else if ( old != val ) {
// if our id has changed ... well this should be ok
Class.list.remove(old);
Class.list.push(this);
}
}
},
/**
* Gets or sets a list of attributes.
* Each attribute is set with [jQuery.Model.prototype.attr attr].
*
* @codestart
* recipe.attrs({
* name: "ice water",
* instructions : "put water in a glass"
* })
* @codeend
*
* @param {Object} [attributes] if present, the list of attributes to send
* @return {Object} the current attributes of the model
*/
attrs: function( attributes ) {
var key;
if (!attributes ) {
attributes = {};
for ( key in this.Class.attributes ) {
if ( this.Class.attributes.hasOwnProperty(key) ) {
attributes[key] = this.attr(key);
}
}
} else {
var idName = this.Class.id;
//always set the id last
for ( key in attributes ) {
if ( key != idName ) {
this.attr(key, attributes[key]);
}
}
if ( idName in attributes ) {
this.attr(idName, attributes[idName]);
}
}
return attributes;
},
/**
* Returns if the instance is a new object
*/
isNew: function() {
return (this[this.Class.id] === undefined); //if null or undefined
},
/**
* Saves the instance if there are no errors.
* If the instance is new, [jQuery.Model.static.create] is
* called; otherwise, [jQuery.Model.static.update] is
* called.
*
* @codestart
* recipe.save(success, error);
* @codeend
*
* If OpenAjax.hub is available, after a successful create or update,
* "<i>modelName</i>.created" or "<i>modelName</i>.updated" is published.
*
* @param {Function} [success] called if a successful save.
* @param {Function} [error] called if the save was not successful.
*/
save: function( success, error ) {
var stub;
if ( this.errors() ) {
//needs to send errors
return false;
}
stub = this.isNew() ? this.Class.create(this.attrs(), this.callback(['created', success]), error) : this.Class.update(this[this.Class.id], this.attrs(), this.callback(['updated', success]), error);
//this.is_new_record = this.Class.new_record_func;
return true;
},
/**
* Destroys the instance by calling
* [jQuery.Model.static.destroy] with the id of the instance.
*
* @codestart
* recipe.destroy(success, error);
* @codeend
*
* If OpenAjax.hub is available, after a successful
* destroy "<i>modelName</i>.destroyed" is published
* with the model instance.
*
* @param {Function} [success] called if a successful destroy
* @param {Function} [error] called if an unsuccessful destroy
*/
destroy: function( success, error ) {
this.Class.destroy(this[this.Class.id], this.callback(["destroyed", success]), error);
},
/**
* Returns a unique identifier for the model instance. For example:
* @codestart
* new Todo({id: 5}).identity() //-> 'todo_5'
* @codeend
* Typically this is used in an element's shortName property so you can find all elements
* for a model with [jQuery.Model.prototype.elements elements].
* @return {String}
*/
identity: function() {
var id = this[this.Class.id];
return this.Class._fullName + '_' + (this.Class.escapeIdentity ? encodeURIComponent(id) : id);
},
/**
* Returns elements that represent this model instance. For this to work, your element's should
* us the [jQuery.Model.prototype.identity identity] function in their class name. Example:
* @codestart html
* <div class='todo <%= todo.identity() %>'> ... </div>
* @codeend
* This function should only rarely be used. It breaks the architecture.
* @param {String|jQuery|element} context -
*/
elements: function( context ) {
return $("." + this.identity(), context);
},
/**
* Publishes to open ajax hub
* @param {String} event
* @param {Object} [opt6] data if missing, uses the instance in {data: this}
*/
publish: function( event, data ) {
this.Class.publish(event, data || this);
},
hookup: function( el ) {
var shortName = underscore(this.Class.shortName),
models = $.data(el, "models") || $.data(el, "models", {});
$(el).addClass(shortName + " " + this.identity());
models[shortName] = this;
}
});
$.each([
/**
* @function created
* @hide
* Called by save after a new instance is created. Publishes 'created'.
* @param {Object} attrs
*/
"created",
/**
* @function updated
* @hide
* Called by save after an instance is updated. Publishes 'updated'.
* @param {Object} attrs
*/
"updated",
/**
* @function destroyed
* @hide
* Called after an instance is destroyed. Publishes
* "shortName.destroyed"
*/
"destroyed"], function( i, funcName ) {
$.Model.prototype[funcName] = function( attrs ) {
var stub;
if ( funcName === 'destroyed' && this.Class.list ) {
this.Class.list.remove(this[this.Class.id]);
}
$(this).triggerHandler(funcName);
stub = attrs && typeof attrs == 'object' && this.attrs(attrs.attrs ? attrs.attrs() : attrs);
this.publish(funcName, this);
return [this].concat($.makeArray(arguments));
};
});
/**
* @add jQuery.fn
*/
// break
/**
* @function models
* Returns a list of models. If the models are of the same
* type, and have a [jQuery.Model.List], it will return
* the models wrapped with the list.
*
* @codestart
* $(".recipes").models() //-> [recipe, ...]
* @codeend
*
* @param {jQuery.Class} [type] if present only returns models of the provided type.
* @return {Array|jQuery.Model.List} returns an array of model instances that are represented by the contained elements.
*/
$.fn.models = function( type ) {
//get it from the data
var collection = [],
kind, ret, retType;
this.each(function() {
$.each($.data(this, "models") || {}, function( name, instance ) {
//either null or the list type shared by all classes
kind = kind === undefined ? instance.Class.List || null : (instance.Class.List === kind ? kind : null);
collection.push(instance);
});
});
retType = kind || $.Model.List || Array;
ret = new retType();
ret.push.apply(ret, $.unique(collection));
return ret;
};
/**
* @function model
*
* Returns the first model instance found from [jQuery.fn.models].
*
* @param {Object} type