Pushing an Object to EmberData hasMany relationship disappears - ember.js

Id like to oush object to a hasMany property of my object. When add it with an action, it shows up, and disappears when the bucket object is saved.
here is some code:
App.Item = DS.Model.extend({
name: DS.attr('string'),
bucket: DS.belongsTo('bucket',{
async: true,
inverse: "items"
}),
});
App.Bucket = DS.Model.extend({
name: DS.attr('string'),
items: DS.hasMany('item',{
async: true,
inverse: "bucket"
}),
});
And this is how I try to push an item to the bucket, in the controller action:
//add new item to the bucket
var bucket = this.get('model');
////////////////
var item = this.store.createRecord('item');
item.set('name', name);
item.save().then(function(resolvedItem){
bucket.get('items').pushObject(item);
bucket.save();
});
Im using ember 1.2.0, and EmberData v1.0.0-beta.3
UPDATE: I'm using Fixture Data.

Related

hasMany relationship not updating in the database

When I save a Block (which belongs to a Cube), the belongsTo relationship is saved but the hasMany relationship for the Cube is not. After creating the Block, no PUT request is being sent to the server to update the Cube.
Block Model:
export default DS.Model.extend({
name: DS.attr(),
description: DS.attr(),
cube: DS.belongsTo('cube')
});
Cube Model:
export default DS.Model.extend({
name: DS.attr(),
description: DS.attr(),
blocks: DS.hasMany('block', { async: true })
});
Saving the Block:
var name = this.get('name');
var description = this.get('description');
var cube = this.store.peekRecord('cube', this.model.id);
if (!name) {return;}
this.store.createRecord('block', {
name: name,
description: description,
cube: cube
})
.save()
.then(function() {
this.resetForm();
var name = slugify(this.model.get('name'));
this.transitionToRoute('cubes.show', this.model.get('id'), name);
}
.bind(this));

How to update model joined with belongsTo?

I have a Consultation model
export default DS.Model.extend({
records: DS.hasMany('record', { async: true }),
currentUser: DS.belongsTo('user'),
remoteUser: DS.belongsTo('user'),
created_time: DS.attr('number'),
freeMsgCount: function () {
return (this.get('remoteUser.msg_block')-this.get('user_msg_cnt'));
}.property('user_msg_cnt'),
.....
});
And User model
export default DS.Model.extend({
name: DS.attr('string'),
.....
});
And I try to update User model. I get json data via WebSocket
socket.on('message', function (jsonObj) {
if (jsonObj.action && jsonObj.action == 'userReload') {
self.store.push('user',jsonObj.userData );
return;
}
}
But Consultation model doesn't know about this update, because I have property freeMsgCount in Consultation model which is using data from User model this.get('remoteUser.msg_block'). User data was updated I saw it in Ember inspector. How can I tell Consultation model to update relation remoteUser?
It seems that you're not watching remoteUser changes in your computed property, so it won't be triggered if remoteUser property's changed. Please add remoteUser.msg_block to computed property declaration:
export default DS.Model.extend({
# ...
freeMsgCount: function () {
return (this.get('remoteUser.msg_block')-this.get('user_msg_cnt'));
}.property('user_msg_cnt', 'remoteUser.msg_block'),

Emberjs - Lazy load hasMany relationship

I got two models set up like this:
App.Conversation = DS.Model.extend({
header : DS.attr('string'),
created_at : DS.attr('date'),
entry : DS.hasMany('Entry')
});
App.Entry = DS.Model.extend({
body : DS.attr('string'),
conversation: DS.belongsTo('Conversation'),
created_at : DS.attr('date'),
});
And routes like this:
App.ConversationsRoute = Em.Route.extend({
model: function() {
return this.store.find('conversation');
}
});
App.ConversationRoute = Em.Route.extend({
model: function(params) {
return this.store.find('conversation', params.id);
}
})
And a Conversation controller like this:
App.ConversationController = Em.ObjectController.extend({
actions: {
loadMore: function() {
}
},
entries: function() {
return this.get('entry');
}.property('model.entry')
});
Now, what I would like to do is only to supply 10 entries sideloaded with each Conversation. That works with no problems and I am able to retrieve all conversations with their hasMany relationship to Entry.
However I would like to lazyload more related entries at a later point by some functionality in my loadMore action, if the user clicks a button in my app.
How can this be achieved? I really can't wrap my head around how to load more entries and sync them to the Conversation model.
Thank you for your help.
Manually fetch the 10 records via find by query, then push them into the entry collection using pushObjects.
var entryCol = this.get('entry');
store.find('entry', {skip:10}).then(function (col){
entryCol.pushObjects(col);
});
Something like this (sorry phone)

Ember.js belongsTo is not displayed in view

I'm trying to display a model, with a hasMany relation and each of those relations has a belongsTo relation.
For some reason, Ember doesnt want to display the belongsTo.
Here are my models:
App.City = DS.Model.extend({
city: DS.attr('string')
});
App.Child = DS.Model.extend({
name: DS.attr('string'),
city: DS.belongsTo('city', {async: true})
});
App.Activity = DS.Model.extend({
children: DS.hasMany('child',{async:true}),
name: DS.attr('string')
});
My template looks like this:
Activity name: {{name}}<br />
{{#each child in children}}
Child name: {{child.name}}<br />
Child city name: {{child.city.name}}
{{/each}}
{{child.city.name}} is empty.
I've created a JSFiddle to illustrate the problem here: http://jsfiddle.net/N2xdx/
In your City fixtures you have:
App.City.FIXTURES = [
{
id: 1,
name: 'Aarhus'
}
];
But your App.City doesn't have a name: DS.attr('string') mapping. Update your model to the following, and all will work:
App.City = DS.Model.extend({
name: DS.attr('string'),
city: DS.attr('string')
});
This is a fiddle with this working http://jsfiddle.net/marciojunior/vDaxt/

Same Model for different relation

I want to use my File Model with two other models.
App.File = DS.Model.extend({
filename: DS.attr('string'),
//related:DS.belongsTo('App.Task' and 'App.Comment'),
});
App.Task = DS.Model.extend({
title: DS.attr('string'),
files:DS.hasMany('App.Files'),
});
App.Comment = DS.Model.extend({
comment:DS.attr('string'),
files:DS.hasMany('App.Files'),
});
The database structure has a related_id and related_type column. How can I set this up to work with ember?