Skip to content

Instantly share code, notes, and snippets.

@benpickles
Created March 1, 2011 14:22
Show Gist options
  • Save benpickles/849190 to your computer and use it in GitHub Desktop.
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`
////////////////
// 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