Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ember Data Internals

tomdale
December 05, 2012

Ember Data Internals

tomdale

December 05, 2012
Tweet

More Decks by tomdale

Other Decks in Programming

Transcript

  1. Store Adapter Serializer Record Record Record Record Server Semantics Application

    Semantics Things that wouldn't change if your backend changed. I.e., ignoring your server protocol, how would you ideally model your data? How do you think about your models? Things that are specific to your server/backend/ persistence layer.
  2. // State for '/posts' App.PostsState = Ember.State.extend({ setupControllers: function() {

    var posts = App.Post.find(); this.set('controller.content', posts); } });
  3. var attr = DS.attr; App.Post = DS.Model.extend({ title: attr('string'), body:

    attr('string'), comments: DS.hasMany('App.Comment'), author: DS.belongsTo('App.User') });
  4. var eddard = App.Person.createRecord({ firstName: "Eddard", lastName: "Stark" }); var

    robb = App.Person.createRecord({ firstName: "Robb", lastName: "Stark" });
  5. isLoaded isDirty isSaving isDeleted isError isNew isValid These are derived

    from the current state object, which is awesome!
  6. • Each record has an associated state manager. • The

    store does not make changes to records directly. • Instead, events are sent to the record. • The record responds based on its current state.
  7. Uncaught Error: <DS.StateManager:ember227> could not respond to event setProperty in

    state rootState.loaded.created.inFlight. = You tried to change a record while it was being saved.
  8. // model/states.js loading: DS.State.create({ // TRANSITIONS exit: function(manager) { var

    record = get(manager, 'record'); record.trigger('didLoad'); }, // EVENTS loadedData: function(manager) { didChangeData(manager); manager.transitionTo('loaded'); } })
  9. • Are relationships saved in the parent or the child?

    • What payloads are sent to what URLs? • What actions map to what HTTP verbs? • What is the name of the primary key? • What are the names of attributes? • Are objects embedded or referred to by ID? Serializer Adapter
  10. DS.JSONSerializer.registerTransform('boolean', { serialize: function(value) { return value ? 'true' :

    'false'; }, deserialize: function(value) { return value === 'false' ? false : true; } }); Serializer
  11. Serializer Add Belongs-To Relationships addBelongsTo: function(hash, record, key, relationship) {

    var id = get(record, relationship.key+'.id'); if (!Ember.none(id)) { hash[key] = id; } }
  12. Serializer Extract Attributes extractAttribute: function(type, hash, attributeName) { var key

    = this._keyForAttributeName(type, attributeName); return hash[key]; }