forked from jupiterjs/jquerymx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.js
More file actions
92 lines (88 loc) · 2.44 KB
/
cookie.js
File metadata and controls
92 lines (88 loc) · 2.44 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
steal('jquery/dom/cookie','jquery/model/list').then(function($){
/**
* @class jQuery.Model.List.Cookie
* @plugin jquery/model/list/cookie
* @test jquery/model/list/cookie/qunit.html
* @download http://jmvcsite.heroku.com/pluginify?plugins[]=jquery/model/list/cookie/cookie.js
* @parent jQuery.Model.List
*
* Provides a store-able list of model instances. The following
* retrieves and saves a list of contacts:
*
* @codestart
* var contacts = new Contact.List([]).retrieve("contacts");
*
* // add each contact to the page
* contacts.each(function(){
addContact(this);
* });
*
* // when a new cookie is crated
* $("#contact").submit(function(ev){
* ev.preventDefault();
* var data = $(this).formParams();
*
* // gives it a random id
* data.id = +new Date();
* var contact = new Contact(data);
*
* //add it to the list of contacts
* contacts.push(contact);
*
* //store the current list
* contacts.store("contacts");
*
* //show the contact
* addContact(contact);
* })
* @codeend
*
* You can see this in action in the following demo. Create a contact, then
* refresh the page.
*
* @demo jquery/model/list/cookie/cookie.html
*/
$.Model.List("jQuery.Model.List.Cookie",
/**
* @Prototype
*/
{
days : null,
/**
* Deserializes a list of instances in the cookie with the provided name
* @param {String} name the name of the cookie to use.
* @return {jQuery.Model} returns this model instance.
*/
retrieve : function(name){
// each also needs what they are referencd by ?
var props = $.cookie( name ) || {type : null, ids : []},
instances = [],
Class = props.type ? $.String.getObject(props.type) : null;
for(var i =0; i < props.ids.length;i++){
var identity = props.ids[i],
instanceData = $.cookie( identity );
instances.push( new Class(instanceData) )
}
this.push.apply(this,instances);
return this;
},
/**
* Serializes and saves this list of model instances to the cookie in name.
* @param {String} name the name of the cookie
* @return {jQuery.Model} returns this model instance.
*/
store : function(name){
// go through and listen to instance updating
var ids = [], days = this.days;
this.each(function(i, inst){
$.cookie(inst.identity(), $.toJSON(inst.attrs()), { expires: days });
ids.push(inst.identity());
});
$.cookie(name, $.toJSON({
type: this[0] && this[0].constructor.fullName,
ids: ids
}), { expires: this.days });
return this;
}
})
})