0

我有这个代码:

    var Day        = Backbone.Model.extend({
        default: {
            service: 'None',
            concert: 'None',
            bible:   'None',
            social:  'None',
            play:    'None'
        }
    }),

    Day_view   = Backbone.View.extend({

        render: function(){
            this.$el.html('Service:' + this.model.service + '<br />Concert:' + this.model.concert + '<br />Bible:' + this.model.bible + '<br />Social:' + this.model.social + '<br />Play:' + this.model.play);
            return this;
        } 
    }),

    day_view = new Day_view({model: new Day({service: '10', concert: '11', bible:   '11', social:  '13', play:    '14' })});
    alert(day_view.render().$el.html());

我明白了:

服务:未定义 音乐会:未定义 圣经:未定义 社交:未定义 播放:未定义

问题出在哪里 ?谢谢

4

2 回答 2

1

在 Backbone 中访问属性的方式是get

this.model.get('service')
于 2013-05-26T21:04:05.573 回答
0

此外,它会抛出未定义的错误,因为您没有定义el视图。您必须将 DOM 元素作为挂钩点传递给 Backbone 视图以插入 html。

$(document).ready(function() {
    var Day = Backbone.Model.extend({
        default: {
            service: 'None',
            concert: 'None',
            bible:   'None',
            social:  'None',
            play:    'None'
        }
    }),

    Day_view = Backbone.View.extend({
        el: 'body',
        render: function(){
            this.$el.html('Service:' + this.model.get('service') + '<br />Concert:' + this.model.get('concert') + '<br />Bible:' + this.model.get('bible') + '<br />Social:' + this.model.get('social') + '<br />Play:' + this.model.get('play'));
            return this;
        } 
    }),

    day_view = new Day_view({model: new Day({service: '10', concert: '11', bible:   '11', social:  '13', play:    '14' })});
    alert(day_view.render().$el);
});
于 2013-05-26T21:59:51.370 回答