Skip to content

Instantly share code, notes, and snippets.

@kumkanillam
Last active June 3, 2016 07:39
Show Gist options
  • Save kumkanillam/bf7f55f632d4b3035c7fba4b1f632918 to your computer and use it in GitHub Desktop.
Save kumkanillam/bf7f55f632d4b3035c7fba4b1f632918 to your computer and use it in GitHub Desktop.
Ember-data- HasMany Not triggering computed properties
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
name: DS.attr('string'),
days: DS.hasMany('day'),
dates: Ember.computed('days.@each', function () {
console.log("dates computed in calendar model ");
return this.get('days').map(day => day.get('date'));
})
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
date: DS.attr('string'),
calendar: DS.belongsTo('calendar')
});
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel(){
this._super(...arguments);
$.mockjax({
url: '/calendars',
type: 'GET',
responseText:{
"data": [{
"type":"calendar",
"id":1,
"attributes": {
"id":1,
"name":"first calendar",
"days":[2,3],
},
"relationships": {
"days": {
"data":[
{"id":2,"type":"day"},
{"id":3,"type":"day"}
]
}
}
},
{
"type":"day",
"id":2,
"attributes":{
"id":2,
"calendar":1,
"date":"2016-06-15"
}
},
{
"type":"day",
"id":3,
"attributes":{
"id":3,
"calendar":1,
"date":"2016-06-17"
}
}]
}
});
$.mockjax({
url: '/red',
type: 'GET',
responseText:{'one':1}
});
$.mockjax({
url: '/days',
type: 'GET',
responseText:{
"data":[{
"type":"day",
"id":2,
"attributes":{
"id":2,
"calendar":1,
"date":"2016-06-15"
}
},
{
"type":"day",
"id":3,
"attributes":{
"id":3,
"calendar":1,
"date":"2016-06-17"
}
}]
}
});
},
model(){
return this.store.findAll('calendar');
},
afterModel(resolvedModel,transition){
this._super(...arguments);
console.log("resolvedmodel");
console.log(resolvedModel);
},
setupController(){
this._super(...arguments);
console.log('setupcontroller');
}
});
<h1>Welcome to {{appName}}</h1>
<br>
Calendar
{{#each model as |cal|}}
Name: {{cal.name}} <br>
{{#each cal.days as |singleday|}}
id:{{singleday.id}} cal: {{singleday.calendar}} date : {{singleday.date}}
<br>
{{/each}}
<br>
The "regular" property
<ul>
{{#each cal.days as |d|}}
<li>{{d.date}}</li>
{{/each}}
</ul>
The computed property
<ul>
{{#each cal.dates as |d|}}
<li>{{d}}</li>
{{/each}}
</ul>
{{/each}}
<br>
{{outlet}}
<br>
<br>
{
"version": "0.8.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"jquery-mockjax":"https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.min.js",
"ember": "2.5.1",
"ember-data": "2.5.2",
"ember-template-compiler": "2.5.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment