【问题标题】:Ember.js Get Model in ViewEmber.js 在视图中获取模型
【发布时间】:2013-12-17 02:31:08
【问题描述】:

我最近开始使用带有 ASP.NET Web API 的 Ember.js。我很难让模型从服务器加载。但是现在我有了它,我想从视图中的商店获取模型。 目前我正在开发一个日历应用程序。这是逻辑,

    App.IndexRoute = Ember.Route.extend({
    //setupController: function (controller) {
    //    // Set the IndexController's `title`
    //    this.controllerFor('application').set('model', App.application.options);
    //}
    model: function () {
        return this.store.findAll('eventList');
    },

    setupController: function (controller, model) {
        this.controllerFor('index');
       // controller.set('model', App.Events.findAll());
    }
});

控制器:

    App.IndexController = Ember.ArrayController.extend({
    sortProperties: ['eventId'],
    sortAscending: false,
    //events: Ember.computed.alias('model')
});

型号

    App.EventList = DS.Model.extend({
    eventId: DS.attr('string'),
    title: DS.attr('string'),
    start: DS.attr('string'),
    end: DS.attr('string'),
    allDay: DS.attr('string')
});

App.EventListSerializer = DS.WebAPISerializer.extend({
    primaryKey: 'eventId',
});

终于看到了

    /// <reference path="../../@JSense.js" />
App.IndexView = Ember.View.extend({
    didInsertElement: function () {

       var events= this.get('controller').get('store');

        //the view has been completely inserted into the dom
        //  $('#calendar').fullCalendar(this.get('controller.model'));
        $('#calendar').fullCalendar({
            events: events
        });

现在我被困在将模型放入视图中。因为一旦didInsertElement 被触发,我想用我从商店收到的模型初始化日历插件。

var events= this.get('controller').get('store'); 不起作用。

但它确实让我明白了:

var events= this.get('controller').get('store');

如您所见,我的模型已经在其中了。那我怎么把它放到视图中呢? 我真的被卡住了,这就是为什么我最终问了这个问题。请帮忙...:)

【问题讨论】:

    标签: javascript asp.net ember.js javascript-framework


    【解决方案1】:

    Ember 有一个默认的 setupController,其中设置了 controller.model,所以如果你覆盖它,你需要这样做:

    setupController: function (controller, model) {
        // setup the model
        controller.set('model', model);       
        // setup other things
    }
    

    此外,当您使用this.get('controller.model') 获取模型时,会返回一个类似对象的数组,称为DS.RecordArray,并且该数组具有DS.Model 的实例,该模型不是普通的javascript 对象,所以如果您的@987654327 @期望一个普通的js对象,你需要在记录数组的每一项中使用model.toJSON()

    这是更新后的代码:

    App.IndexRoute = Ember.Route.extend({    
        model: function () {
            return this.store.findAll('eventList');
        },
        setupController: function (controller, model) {
            controller.set('model', model);       
        }
    });
    
    App.IndexView = Ember.View.extend({
        didInsertElement: function () {
            var events = this.get('controller.model').map(function(record) {
                return record.toJSON({ includeId: true });
            });
            $('#calendar').fullCalendar({
                events: events
            });
        }
    });
    

    希望对你有帮助

    【讨论】:

    • 确实有效,但不是说record.toJSON?我不得不说record._data
    • 嗯我不推荐使用_data,这是内部的,可以改变。 toJSON 抛出一些错误?
    • 哦,不,它有效!!!当我尝试时,首先,我得到了错误,一定是我这边有问题......
    猜你喜欢
    • 2012-12-26
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多