hasMany relationship not updating in the database - ember.js

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));

Related

Uncaught Error: Assertion Failed: You may not set `id` as an attribute on your model --- want to duplicate id attribute

I have a model called group having multiple attributes, I also want to save my id information to some other attribute because of some reasons to pass the model info to somewhere else.
import DS from 'ember-data';
var group = DS.Model.extend({
groupId : DS.attr('string'),//want to duplicate id here
groupName: DS.attr('string'),
groupRegion: DS.attr('string'),
groupCountry: DS.attr('string'),
groupSegment: DS.attr('string'),
groupSubSegment: DS.attr('string'),
isSelected: DS.attr('boolean'),
isExpand: DS.attr('boolean')
});
export default group;
You can make use of the ready hook.
var group = DS.Model.extend({
groupId : DS.attr(),//want to duplicate id here
groupName: DS.attr('string'),
groupRegion: DS.attr('string'),
groupCountry: DS.attr('string'),
groupSegment: DS.attr('string'),
groupSubSegment: DS.attr('string'),
isSelected: DS.attr('boolean'),
isExpand: DS.attr('boolean'),
ready(){
this.set('groupId',this.get('id'));
}
});

Pushing an Object to EmberData hasMany relationship disappears

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.

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?

Ember-Data. Add Child record for many2many & one2many

JSFiddle - http://jsfiddle.net/9gA4y/1/
I have following model:
contact => (many2many) => tags
contact => (one2many) => address
Ember Data Model:
App.Contact = DS.Model.extend({
name: attr('string'),
tags: hasMany('App.Tag'),
addresses: hasMany('App.Address')
});
App.Address = DS.Model.extend({
street: attr('string'),
country: attr('string'),
contacts: belongsTo('App.Contact')
})
App.Tag = DS.Model.extend({
name: attr('string'),
contacts: hasMany('App.Contact')
});
I figured out adding New contact record
How do I associate existing, Address to Newly created contact. (one 2 Many)
How do I associate existing, Tags to Newly created contact. (Many 2 Many)
How do I delete associations in a existing contact.
hasMany relationships can be manipulated through addObject, addObjects or removeObject.
contact.get('addresses').pushObject(address);
contact.get('addresses').removeObject(address);
You could also set contact on the address
address.set('contact', contact);
address.set('contact', null);
Also, you note that you should use the singular form for a belongsTo association (contact not contacts):
App.Address = DS.Model.extend({
street: attr('string'),
country: attr('string'),
contact: belongsTo('App.Contact')
});
Try:
var contact = App.Contact.find(1),
address = App.Address.find(1),
tag = App.Tag.find(1);
contact.get('addresses').addObject(address);
contact.get('tags').addObject(tag);
this.get('store').commit(); //saves address and tag to contact
contact.get('tags').removeObject(tag);
this.get('store').commit(); //removes tag from contact