1

私のバックボーンビューでは、タグ名、クラス名、一時値を設定しています。クラス名以外はすべて正常に機能します。

クラス名を設定するにはどうすればよいですか。またはコードの間違いは何ですか。

define(["singleton","listCollection","listModel"],function(singleton,collection,listModel){
    singleton.view = Backbone.View.extend({
        tagName     :'article',
        className   :'indBoard',
        projectName : true,
        template0   : _.template($('#listTemplate').html()),
        template1   : _.template($('#boardTemplate').html()),
        initialize  :function(options){
            this.template = this['template'+options.tempNo];
            this.tagName = options.tagName;
                    //i am changing to 'li' works
            this.className = options.cName; 
                    //changing to new class name not working
            console.log(options.cName);//consoles new class name properly
                  this.projectName = options.subTempNo == 0 ?true:false;                 
                   //condition as well works..
        },
        render:function(){
            var temp = this.template;
            this.$el.html(temp(this.model.toJSON()));
            return this;
        }
    });
    return singleton.view;
});
4

1 に答える 1

4

options.classNameの代わりにビューインスタンスを作成するときに設定する場合は、そのようoptions.cNameに設定する必要はありませんinitialize(tagNameについても同じです)。

代わりに次のようなものを試してください。

var view = new singleton.view({className: 'someClass'});

classNameは、ビューの作成中にBackboneが検索する特別なオプションの1つです。

バックボーンソースから:

// List of view options to be merged as properties.
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events'];

実際、tagNameあなたのために働いている理由は、Backboneによってマージされているためであり、で設定しているためではないと思いますinitialize

于 2013-02-07T05:56:57.910 に答える