Reputation: 41
Models A User has many Companies (one to many relationship). Ember models:
// User model
App.User = DS.Model.extend({
companies: DS.hasMany("company", {async: true})
});
App.Company = DS.Model.extend({
user: DS.belongsTo("user")
});
Problem When I create a new company model, define its user property and save() it, the User's companies property does not update until I refresh the page. Example:
var user = this.get("controllers.application").get("model"); // Get user
company.set('user', user); // Set user to current user
company.save(); // Call POST HTTP verb to update db model
However, as a work around, i can explicitly push the new Company model into the users list of companies, to see the new company straight away. I do not call save() after i do this. But is this step necessary?
user.get("companies").pushObject(company); // Add company to users list of companies
App Details
- Using Ember RESTAdapter
- Flask Server on backend with Flask-SQLAlchemy
- Using Flask-Restless
Upvotes: 0
Views: 260
Reputation: 41
The problem was what model I was assigning to this route. I was previously assigning the logged in User as the model, and accessing their list of companies like:
App.MyCompaniesRoute = Ember.Route.extend({
model: function(){
return this.store.find("user", 1); // ID is 1 for example
}
});
Where as I should have been setting the model for this route as all the companies in the store that have the same user as the currently logged in User, e.g.:
model: function(){
this.store.filter("company", function(company){
return company.get("user") == currentUser; // Current user model
});
}
Thanks!
Upvotes: 0