Created
March 1, 2011 14:22
-
-
Save benpickles/849190 to your computer and use it in GitHub Desktop.
js-model `uid`s persist over localStorage page refreshes and are useful when you don't have a real `id`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////// | |
// IN CONSOLE // | |
//////////////// | |
Blah = Model("blah", { | |
persistence: Model.localStorage(), | |
find_by_uid: function(uid) { | |
return this.detect(function() { | |
return this.uid == uid | |
}) | |
} | |
}) | |
blah = new Blah({ name: "Blah" }) | |
blah.uid | |
// => "blah-1298988849274-662" | |
blah.save() | |
Blah.all() | |
// => [blah] | |
////////////////// | |
// REFRESH PAGE // | |
////////////////// | |
Blah = Model("blah", { | |
persistence: Model.localStorage(), | |
find_by_uid: function(uid) { | |
return this.detect(function() { | |
return this.uid == uid | |
}) | |
} | |
}) | |
Blah.all() | |
// => [] | |
Blah.load() | |
Blah.all() | |
// => [blah] | |
// Use the uid returned earlier. | |
Blah.find_by_uid("blah-1298988849274-662") | |
// => blah |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment