Created
April 13, 2012 19:56
-
-
Save ppcano/2379657 to your computer and use it in GitHub Desktop.
How to eliminate the following unnecesary code with ember-data
This file contains hidden or 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
| // I need to identify if the app is in a callback environment or fixtures | |
| // ending with the following code which seems not to be the correct way to do that | |
| insertCode: function(stateManager, code ) { | |
| var invitations = Yn.store.find(Yn.Invitation, {code: code}); | |
| var self = this; | |
| invitations.addObserver('isLoaded', function() { | |
| self._callback(stateManager, invitations); | |
| }); | |
| if ( !Yn.WORKING_WITH_SERVER ) { | |
| self._callback(stateManager, invitations); | |
| } | |
| }, | |
| _callback: function(stateManager, invitations) { | |
| var status = 0; | |
| if ( invitations.get('length') > 0 ) { | |
| App.invitationController.set('content', invitations); | |
| App.invitationController.set('selected', invitations.objectAt(0) ); | |
| stateManager.goToState('invitation'); | |
| } else { | |
| status = 1; | |
| } | |
| App.readController.set('status', status); | |
| } |
This file contains hidden or 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
| //// rest adapter | |
| findAll: function(store, type) { | |
| var root = this.rootForType(type); | |
| this.ajax(root, "GET", { | |
| success: function(json) { | |
| store.loadMany(type, json["objects"]); | |
| store.typeMapFor(type).modelArrays.forEach( function(item) { | |
| item.set('isLoaded', true); | |
| }); | |
| } | |
| }); | |
| }, | |
| ///// fixture adapter | |
| findAll: function(store, type) { | |
| var fixtures = type.FIXTURES; | |
| ember_assert("Unable to find fixtures for model type "+type.toString(), !!fixtures); | |
| var ids = fixtures.map(function(item, index, self){ return item.id; }); | |
| store.loadMany(type, ids, fixtures); | |
| store.typeMapFor(type).modelArrays.forEach( function(item) { | |
| item.set('isLoaded', true); | |
| }); | |
| } | |
Author
Mmm, instead of
var invitations = Yn.store.find(Yn.Invitation, {code: code});
var self = this;
invitations.addObserver('isLoaded', function() {
self._callback(stateManager, invitations);
});
if ( !Yn.WORKING_WITH_SERVER ) {
self._callback(stateManager, invitations);
}
I do something like
var invitations = Yn.store.find(Yn.Invitation, {code: code});
if (invitations.get('isLoaded')) {
// call callback function directly
} else {
// add observer on 'isLoaded' with callback function
}
We definitely needs normal events.
Author
thanks, much better. Anyway, seems than ember-data could provides something to not require the isLoaded patch.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should not it be easier provides store calls with callback functions?