Search code examples
ember.jsember-datafixtures

Access and Update a Fixture Ember JS


How can I update/edit a specific Person in IndexController?

App.Person=DS.Model.extend({
        name:DS.attr('string'),
        iminus:DS.attr(),
        iplus:DS.attr()
    });

    App.Person.FIXTURES=[
        {
            id:1,
            name:'M',
            iminus:[{amount:50,pers:'A'}],
            iplus:[]
        },
        {
            id:2,
            name:'An',
            iminus:[{amount:20,pers:'Mo'}],
            iplus:[]
        }
    ];
    App.IndexRoute=Ember.Route.extend({
        model:function(){
            return this.store.findAll('person');
        }
    });
    App.IndexController=Ember.ArrayController.extend({
    actions:{
        createNew:function(){
            //Access and Update a person record based on name
        }
    }
    });

How can I access and update the person record based on a name inside IndexController


Solution

  • var App = Ember.Application.create();
    
    App.FixtureAdapter = DS.FixtureAdapter.extend({
        queryFixtures: function (records, query, type) {
            return records.filter(function (record) {
                for (var key in query) {
                    if (!query.hasOwnProperty(key)) {
                        continue;
                    }
                    var value = query[key];
                    if (record[key] !== value) {
                        return false;
                    }
                }
                return true;
            });
        }
    });
    
    App.ApplicationAdapter = App.FixtureAdapter;
    
    App.Router.map(function () {
    
    });
    
    
    App.Person = DS.Model.extend({
        name: DS.attr('string'),
        iminus: DS.attr(),
        iplus: DS.attr()
    });
    
    App.Person.reopenClass({
        FIXTURES: [{
            id: 1,
            name: 'M',
            iminus: [{
                amount: 50,
                pers: 'A'
            }],
            iplus: []
        }, {
            id: 2,
            name: 'An',
            iminus: [{
                amount: 20,
                pers: 'Mo'
            }],
            iplus: []
        }]
    });
    
    App.IndexRoute = Ember.Route.extend({
        model: function () {
            return this.store.findAll('person');
        }
    });
    
    App.IndexController = Ember.ArrayController.extend({
        actions: {
            updatePerson: function (name) {
                var person;
    
                this.get('model').filter(function (model) {
                    if (model.get('name') === name) {
                        person = model;
                    }
                });
    
                var iminus = person.get('iminus');
                iminus.push({
                    amount: 100,
                    pers: 'Mo'
                });
    
                person.setProperties({
                    iminus: iminus
                });
    
                person.save()
                    .then(success)
                    .catch (fail);
    
                function success(model) {
                    alert('success');
                }
    
                function fail(error) {
                   alert('fail');
                }
    
            }
        }
    });
    

    http://emberjs.jsbin.com/wamiminesi/1/edit?html,js,output