0

我正在尝试从两个模型(具有 hasMany 和 belongsTo 关系)中设置数据并将它们保存到 firebase。

'list' 数据最终被保存到 firebase 但不是用户数据。

我认为我在第 3 步做错了。感谢您的帮助!

import Ember from 'ember';

export default Ember.Route.extend({
    model: function() {
        return this.store.find('list');
    },
actions: {
    createList: function() {
        var newListTitle = this.controllerFor('lists').get('newListTitle');
        var username = this.get('session.user.displayName');
        alert(this.get('session.user.displayName'));

        if (Ember.isBlank(newListTitle)) { return false; }

    //1
        var list = this.store.createRecord('list', {
            title: newListTitle,
            user: username,
        });

    //2
        this.controllerFor('lists').set('newListTitle', '');

        var _this = this;

    //3
            list.save().then(function(list) {
                user.get('lists').addObject(list);
                user.save();
                _this.transitionTo('lists.show', list); //4
            });

        }
    }
});
4

1 回答 1

0

重组了您的添加逻辑以及用户定义的模型,还修改了您的路线,在编辑和查看模式下可能看起来像这样。这意味着您可以从“模型”返回多个项目。

// Update models

App.List = DS.Model.extend({
     value:     DS.attr('string')
});

App.User = DS.Model.extend({
    name:   DS.attr('string')
});

App.UserLists = DS.Model.extend({
   user: DS.belongsTo('user'),
   list: DS.belongsTo('list')
});



    export default Ember.Route.extend({
        LIST:SHOW ROUTE
        model: function(params) {

            var store = this.get('store');
            var userPromise = store.find('user', params.id);

            return Ember.RSVP.hash({
                user: userPromise,
                userList : userPromise.then(function(user) {
                    return store.find(userList, { WhereUserIdIs : user.get('id') })
                });
            });
        },
    actions: {
        createList: function() {
            var self = this;

            var failure = function(reason) {
                // handle stuff
            };

            var list = this.store.createRecord('list', {
                title: this.get('title'),
            });

            var user = this.get('user');

            var usersList = store.createRecord('userList', {
                'user': user,
                'list': list
            });

            list.save().then(function(list) {
                user.save().then(function() {
                    userList.save().then(function() {
                        self.transitionTo('lists.show', list.get('id'));
                    }, failure);
                }, failure);
            }, failure);

        }
    });
于 2015-04-28T11:10:10.073 回答