New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ember-data-partial-model

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-data-partial-model

Add support for partial models to Ember Data.

0.3.0
Source
npm
Version published
Weekly downloads
19
-87.25%
Maintainers
1
Weekly downloads
 
Created
Source

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

import { PartialModel, partial } from 'ember-data-partial-model/utils/model';
const { attr } = DS;

export default PartialModel.extend({
  name: attr(),
  extended: partial('user', 'extended', {
    twitter: attr()
  })
});

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.find('user'); // goes to /users
let user = users.objectAt(1);

user.get('extended') //goes to /users/1, returns a promise
user.get('twitter') //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

{{user.twitter}}

and it will populate once the full user is loaded

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

import DS from 'ember-data';
import PartialModelAdapter from 'ember-data-partial-model/mixins/adapter';
const { JSONAPIAdapter } = DS;

export default JSONAPIAdapter.extend(PartialModelAdapter);

For serializer (both JSONAPISerializer and RESTSerializer are available):

// app/serializers/application.js

import DS from 'ember-data';
import PartialModelJSONAPISerializer from 'ember-data-partial-model/mixins/jsonapi-serializer';
const { JSONAPISerializer } = DS;

export default JSONAPISerializer.extend(PartialModelJSONAPISerializer);

or

// app/serializers/application.js

import DS from 'ember-data';
import PartialModelRESTSerializer from 'ember-data-partial-model/mixins/rest-serializer';
const { RESTSerializer } = DS;

export default RESTSerializer.extend(PartialModelRESTSerializer);

For store:

// app/services/store.js

import DS from 'ember-data';
import PartialModelStore from 'ember-data-partial-model/mixins/store';
const { Store } = DS;

export default Store.extend(PartialModelStore, {});

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:

import Ember from 'ember';
import baseSerializerMixin from 'ember-data-partial-model/mixins/base-serializer';
const { Mixin } = Ember;

export default Mixin.create(baseSerializerMixin, {
  _normalizePartialRelationship: function(modelClass, resourceHash, descriptor) {
    // add this function to handle normalization of partial extension as a relationship.
  },

  _copyAttributesFromPartialToParent: function(serializedHash, partialHash) {
    // add this function to handle copying attributes from partial extension to top-level hash.
  },
});

Installation

  • git clone this repository
  • npm 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/.

Keywords

ember-addon

FAQs

Package last updated on 22 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts