Skip to content

Instantly share code, notes, and snippets.

@shiroginne
Created August 7, 2013 20:43
Show Gist options
  • Save shiroginne/6178459 to your computer and use it in GitHub Desktop.
Save shiroginne/6178459 to your computer and use it in GitHub Desktop.
ember.js app
App = Ember.Application.create();
App.Router.map(function() {
this.resource("questions", { path: '/'}, function () {
this.resource("question", { path: ':question_id'})
});
this.resource("result")
});
App.QuestionsRoute = Ember.Route.extend({
setupController: function (controller, values) {
controller.set('model', App.Question.find());
}
});
App.ResultRoute = Ember.Route.extend({
setupController: function(controller, song) {
controller.set('model', result);
}
})
App.QuestionsController = Ember.ArrayController.extend({});
App.ApplicationController = Ember.ObjectController.extend({
calculatedResult: function () {
// How to get values from all Answers where isSelected is true?
}.property('calculatedResult')
})
App.Store = DS.Store.extend({
revision: 13,
adapter: DS.FixtureAdapter.create()
});
App.Answer = DS.Model.extend({
text: DS.attr('string'),
value: DS.attr('integer'),
isSelected: DS.attr('boolean')
})
App.Question = DS.Model.extend({
number: DS.attr('integer'),
text: DS.attr('string'),
answers: DS.hasMany('App.Answer')
});
// FIXTURES
App.Answer.FIXTURES = [
{ id: 1, text: 'test', value: 1},
{ id: 2, text: 'some text', value: 0},
{ id: 3, text: 'almost true', value: 7},
{ id: 7, text: 'some critics', value: 3},
{ id: 10, text: 'Kate and Leopold', value: 10, isSelected: true }
]
App.Question.FIXTURES = [
{ id: 1, number: 1, text: 'To be or not to be?', answers: [1, 2]},
{ id: 2, number: 2, text: 'Who is on duty today?', answers: [3, 7, 10]}
];
@shiroginne
Copy link
Author

  calculatedResult: function () {
    var summ = 0;
    var ans = App.Answer.find();

    ans.filterProperty('isSelected', true).forEach(function(item) {
      summ = summ + item.get('value');
    });

    return summ;
  }.property('calculatedResult')

but it render 0 :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment