Lets say I have a model like this:
App.User = DS.Model.extend({
attributes : DS.attr('string'),
countries : DS.hasMany('country', { async: true }),
)};
And the server returns the JSON, with a country_idsarray which all works fine, but I don't want to actually load the countries models corresponding to these IDs, which ember-data is doing automatically. Is there any way to stop/suppress this automatic functionality?
Ember Data should load models only if you address to countries field.
If you need only ids try to define country_ids field in model.
UPD:
It seems like *_ids is reserved by framework. So you can ask your server to send this array with another name and define simple attr for this in model.
If you use rails and ActiveModel::Serializer it can be done like this:
class UserSerializer < ActiveModel::Serializer
embed :ids
attributes :id, :attributes
has_many :countries, key: :countries
end
And model:
App.User = DS.Model.extend({
attributes : DS.attr('string'),
countries : DS.attr()
)};
Related
After specifying the relationship between
App.Post = DS.Model.extend({
comments: DS.hasMany('comment')
});
App.Comment = DS.Model.extend({
post: DS.belongsTo('post')
});
But what it does is it accepts integer id in 'post' and an array of integers in 'comments' can anyone please tell me how to use any other variable for the reference like name or title instead of array.
With the associations, they are referencing the ember models not their specific types. So in this case the comments in the post model are just references to the individual comment ember objects
How do I define multiple relationships between two objects in Ember? For example A user can be either a staff or a customer the differentiator is the userType property. If a customer buys a product, the product object need to have link to the customer that bought it and the staff that facilitated the sale.
Here is a basic version of a data model that would suffice for what you have described. You can adapt it for your needs. I have used the app global haven't specified whether you are using ember-cli.
App.User = DS.Model.extend({
name: DS.attr('string'),
userType: DS.attr('string')
});
App.Order = DS.Model.extend({
orderedBy: DS.belongsTo('user'),
facilitatedBy: DS.belongsTo('user')
});
App.Product = DS.Model.extend({
name: DS.attr('string')
});
A useful tool for creating a first pass of a data model when you are unsure of the ember syntax is Ember Data Model Maker. You can use it to see how you should set up your model definitions and then modify them later.
I am transitioning from Ember Data 0.13 to Ember Data 1.0 (beta 1). It seems that the URL constructed for a model is capitalized when it shouldn't. In ED 0.13, capitalization and pluralization occurred automatically and without problems. I suppose the same is still true in ED 1.0, but I must be overlooking something.
App.Account = DS.Model.extend({
// Attributes
company: DS.attr('string'),
// Relationships
users: DS.hasMany('User')
});
App.AccountAdapter = DS.RESTAdapter.extend({
namespace: 'api',
});
In the controller, I create a new record, populate it, and save it.
var account = this.store.createRecord('account');
account.set('company', this.get('company'));
account.save();
The request URL that Ember Data uses for saving the record is http://localhost:3000/api/Accounts. Why is the plural of the model name capitalized? How do I configure the model/adapter to use accounts instead of Accounts?
It appears that a change in the naming convention of model associations is the cause of this issue. Before ED 1.0, an association was declared as shown below.
App.Account = DS.Model.extend({
// Attributes
company: DS.attr('string'),
// Relationships
users: DS.hasMany('App.User')
});
In ED 1.0, however, there is no need to use App.User to declare an association. Passing the model's name, user suffices. Because I capitalized the name of the Account model in the User model, ED capitalized the name (plural) of the model in the URL.
App.User = DS.Model.extend({
// Attributes
first_name: DS.attr('string'),
last_name: DS.attr('string'),
email: DS.attr('string'),
password: DS.attr('string'),
password_confirmation: DS.attr('string'),
// Relationships
account: DS.belongsTo('account') // model name should be lowercase
});
Just like you need to pass the lowercase name of the model for creating a new record (this.store.createRecord('user');), you also need to use the lowercase model name to specify the model for an association.
Say you have a model:
App.Contact = DS.Model.extend
name: DS.attr('string')
addresses: DS.hasMany('App.Address')
I know I can grab the addresses by simply doing:
#get('model').get('addresses')
But say you have many models, each having one or more different hasMany relationships. Is there a way to generically grab the relationships?
Something like:
#get('model').get('hasMany')
Given an ember model class, you can get to it's of the relationships like:
var attributes = Ember.get(App.Post, 'attributes');
var relationships = Ember.get(App.Post, 'relationships');
If you don't know the type of model, you can replace App.Post with
var klass = model.get('constructor');
//or based on your question
var klass = this.get('model.constructor');
I would like to have model binding relationships loaded automatically when referenced in a template. For example, if I have models like this:
App.User = DS.Model.extend
name: DS.attr 'name'
App.Contact = DS.Model.extend
addedBy: DS.belongsTo 'App.User'
and a view like this:
<div>{{contact.addedBy.name}}</div>
it would be really nice if ember-data caught on that it needs to load the User with a primary key in "addedBy". Currently I have to load the User manually with App.User.find(contact.get('addedBy')) and then the template binding updates to display the user's name.
This is a very simple example but in practice I sometimes find myself traversing relationships pretty far. Is there an easy way to automate this?
Thanks folks!
What about side-loading the associated users when serving contacts?
Assuming you are using Rails & active_model_serializers gem, you would have a ContactSerializer like this:
class ContactSerializer < ActiveModel::Serializer
embed :ids, :include => true
#...
has_one :user
end
Doing so, the user will be auto-populated when contact is retrieved.
See documentation.
Turns out that ember-data does exactly what I want by default, and the problem was a bug in my code.
Make sure that the backend for your adapter's findMany() method returns the records in the same order as the argument array of IDs otherwise your DS.hasMany relationships will act very very weird!