Ember Data 2.0-Model States and Flags
Published on Dec 23, 2015
This was going to be a simple episode looking at ‘isEmpty’, ‘isValid’, and methods like that... but digging into them uncovered the far more interesting ‘currentState’, which lies behind not only them but over a dozen separate properties and events on DS.Model.
In this episode we explore the hierarchy of states using my Model State Explorer, seeing how this structure interacts with various properties and events (but mostly the properties) on DS.Model.
When I first started researching this episode, I was looking into all these different properties which were seemingly related, but on the surface it was difficult to tell how.
So all these
is xproperties are telling you something about the state of the model, of the data that you’re working with. And turns out there’s a private property called the
currentState, and there are a select number of states that it can be in that are in a specific hierarchy. So we’re going to go over the hierarchy today and how it demonstrates to all of these properties.
To help us do that, I’ve built a demonstration. This demonstration, it has all these, and it maps out which flags are true in which state. You can find this demonstration at this address and go over it yourself. The rest of this presentation will be pointing out patterns that I’ve noticed.
So here’s some obvious things.
isDeleted is only true in the deleted branch of the hierarchy.
isDirty is true in a surprising number of places. Basically it’s not dirty, this is easier to say. It’s not dirty if it’s empty, if it’s loading, if it’s loaded and saved, or if it’s deleted and saved. Everything else, dirty.
Empty is just an empty state.
isLoading is just in a loading state.
isLoaded is everything else. And so
isLoading form a set that is... it covers everything except root and it doesn’t overlap at all.
isNew, unsurprisingly, it’s in the created state and its children, might also be the invalid here, I’m not sure. By the way, if any of you are heavily involved with Ember data core and you see a problem here, go ahead and shoot me an e-mail and/or a pull request and I will correct this.
isSaving is only true in the various
inFlight states, and
isValid is true everywhere except where it’s explicitly not true.
There’s some other things that are also affected by the state. For example, the
dirtyType can be created, updated, or deleted. And you can guess, the
dirtyType will be deleted here, created here, and updated here.
Of course a model doesn’t stay in one state. It transitions to other states. This happens at the most direct level through the
transitionTo. That’s a private method. Don’t use it. Notice it disappears when I uncheck ‘Private’. And this
transitionTo is usually called by one of the events, which are public methods. And these usually get called by the internal model or something like that.
Anyway, there’s a lot there and I’m not going to cover all of it in this episode. Just if you’re wanting to dig really deep, that’s a good place to start for transitions.
So to make this app, to get all this information, I dug pretty deep into the source code and I also did a few cool tricks with the Ember code. So next week, I’m going to give you a quick tour of the
states.js file, where I got all this information. Alright, I will see you then.
- Ember Data 2.0-Getting Started, and Basics of DS.Model
- Ember Data 2.0-Getting Data from the Server with findRecord and findAll
- Ember Data 2.0-Store Manipulation with Peek, Unload, and More
- Ember Data 2.0-Create, Save, and Destroy Records
- Ember Data 2.0-Updating Data, Tracking Changes, and Rolling Them Back
- Ember Data 2.0-Metaprogramming with DS.Model Attributes Property
- Ember Data 2.0-Model States and Flags
- Ember Data 2.0-states.js Deep Dive
- Ember Data 2.0-Relationships
- Ember Data 2.0-Metaprogramming with Relationships
- Ember Data 2.0-Overview of using Adapters and Serializers
- Ember Data 2.0-Overview of Customizing Adapters and Serializers
- Ember Data 2.0-Essential Adapter Customizations
- Ember Data 2.0-Advanced Adapter Customizations
- Ember Data 2.0-Miscellaneous Adapter Customizations
- RESTAdapter vs JSONAPIAdapter vs ActiveModelAdapter
- Introduction to Serializers
- JSON API
- Serializers-How are normalize and normalizeResponse different?
- Serializers-Extracting Attributes and IDs
- Serializers-Extracting Relationships
- Serializers-keyForAttribute and keyForRelationship