Ember-data-partial-model
This addon adds support for partial records to Ember Data. Let's say your api for
/users
returns a shallow model like:
id:1 name: 'BMac' id:2 name:'Seb'
but /users/1
returns a detailed model
id:1 name: 'BMac' twitter: 'seb'
You can use this addon to define your User model as:
// app/models/user.js ;const attr = DS; ;
This addon will modify your application adapter/serializer, so that accessing user.get('extended')
or any of the extended properties will trigger a request for the detailed model.
For example:
let users = await store; // goes to /userslet user = users; user //goes to /users/1, returns a promiseuser //goes to /users/1, but is just an alias, so returns null initially, should not do in app code
Thus you are safe from race conditions when rendering users, even if you have not loaded the full user In your template you can do
usertwitter
and it will populate once the full user is loaded
Introduction Video
How to use
If you haven't customized any of your adapters, serializers or store, the addon will work out of the box. By default it uses JSONAPISerializer and JSONAPIAdapter.
If you made any customization, you will need to include proper mixins. For adapter:
// app/adapters/application.js ;;const JSONAPIAdapter = DS; ;
For serializer (both JSONAPISerializer and RESTSerializer are available):
// app/serializers/application.js ;;const JSONAPISerializer = DS; ;
or
// app/serializers/application.js ;;const RESTSerializer = DS; ;
For store:
// app/services/store.js ;;const Store = DS; ;
Supporting custom serializers
If you are using different serializer than REST or JSONAPI, you can create custom mixin to support it, you will just need to provide some functions for handling logic specific to your serializer. The basic layout is the following:
;;const Mixin = Ember; ;
Extending partial serializers
You can provide extensions and list of mixins to be applied for partial serializers. If you defined extended
partial in user
model like this:
// app/models/user.js;const attr = DS;;
add you want to make clients
relationship embedded and serialize all clients
records when serializing user
model, you could customize user
serializer the following way:
// app/serializers/user.js;;;const EmbeddedRecordsMixin = DS;const Mixin = Ember;;
All extensions defined inside partialSerializersExtensions
will be copied for given partial and all mixins defined in partialSerializersMixins
will be used when defining partial serializer.
Installation
git clone
this repositorynpm install
bower install
Running
ember server
- Visit your app at http://localhost:4200.
Running Tests
ember test
ember test --server
Building
ember build
For more information on using ember-cli, visit http://www.ember-cli.com/.