Skip to content

Instantly share code, notes, and snippets.

@ppcano
Created April 13, 2012 19:56
Show Gist options
  • Select an option

  • Save ppcano/2379657 to your computer and use it in GitHub Desktop.

Select an option

Save ppcano/2379657 to your computer and use it in GitHub Desktop.
How to eliminate the following unnecesary code with ember-data
// 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);
}
//// 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);
});
}
@ppcano

ppcano commented Apr 13, 2012

Copy link
Copy Markdown
Author

Should not it be easier provides store calls with callback functions?

@caligo-mentis

Copy link
Copy Markdown

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.

@ppcano

ppcano commented Apr 14, 2012

Copy link
Copy Markdown
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