Created
December 17, 2012 23:38
-
-
Save ncolgan/4323483 to your computer and use it in GitHub Desktop.
Singleton resources in ember.js
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
App.Store = DS.Store.extend({ | |
revision: 10, | |
adapter: DS.RESTAdapter.extend({ | |
bulkCommit: false, | |
find: function(store, type, id) { | |
if (id === 'singleton') { | |
var url = '/' + this.rootForType(type); | |
$.getJSON(url, function(data) { | |
data.id = id; | |
store.load(type, id, data); | |
}); | |
} else { | |
this._super.apply(this, arguments); | |
} | |
} | |
}) | |
}) | |
App.Profile = DS.Model.extend({ | |
first_name: DS.attr('string'), | |
last_name: DS.attr('string') | |
}); | |
App.Profile.reopenClass({ | |
find: function() { | |
return this._super('singleton'); | |
} | |
}); |
Note for folks now in 2014 using Ember 1.7, and Data 1.0 (or abouts), the
store.load type, id, data
part should be replaced with the following
store.push type, data
since the newer versions of ember no longer supports store.load
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks like a nice solution. Thank you for sharing