简体   繁体   English

Backbone.js绑定动态EL生成

[英]Backbone.js binding dynamic el generation

I have a small problem where I am creating a view and dynamically creating the "el" element and passing it on to the view that I am creating. 我在创建视图并动态创建“ el”元素并将其传递给正在创建的视图时遇到了一个小问题。

var divIdentification = ".data-identification-"+this.model.dataTypeId+"-"+this.model.dataGrouping.get("data1Id")+"-"+this.model.dataGrouping.get("data2Id");

THE FOLLOWING WORKS 以下工作

var roomOccupancyView = new Bookings.RoomOccupancyView({
                    model: datas,
                    el: "div"
                });

HOWEVER THE FOLLOWING DOES NOT 但是以下内容没有

var divIdentification = ".data-identification-"+this.model.dataTypeId+"-"+this.model.dataGrouping.get("data1Id")+"-"+this.model.dataGrouping.get("data2Id");

    var roomOccupancyView = new Bookings.RoomOccupancyView({
                        model: datas,
                        el: divIdentification 
                    });

When I try placing a console.log for el in the child view for the first scenario where I pass el as a string I get the following object 当我尝试在第一个方案中将el作为字符串传递的第一种情况在子视图中放置el的console.log时,得到以下对象

<div class="header-container">

HOWEVER when I pass the variable I get "undefined". 但是,当我传递变量时,会得到“未定义”。

Additionally I also tried wrapping the variable output as $(string) however that too did not work as the console.log still provides a different output. 另外,我还尝试将变量输出包装为$(string),但是由于console.log仍然提供了不同的输出,因此也没有用。

Does anyone have any idea to pass a dynamic value into the el attribute value. 没有人有任何想法将动态值传递给el属性值。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Regards, MilindaD 此致MilindaD

As pointed out by the comment, the el attribute passed in through initializer function can only be a String or jQuery object. 正如评论所指出的那样,通过初始化函数传递的el属性只能是String或jQuery对象。 Otherwise, Backbone will create an el from it's tagName , className and id attribute. 否则,Backbone将根据其tagNameclassNameid属性创建一个el。

So in your first scenario, you passed el: "div" and Backbone will then bind the view's el to the first div element in the DOM. 因此,在您的第一个场景中,您传递了el: "div" ,然后Backbone将视图的el绑定到DOM中的第一个div元素。

In your second scenario, you passed el: divIdentification and Backbone will set view's el to $(divIdentification) which is not defined in your DOM. 在第二种情况下,您通过了el: divIdentification和Backbone会将视图的el设置为$(divIdentification),这在您的DOM中未定义。

In order for you to set el dynamically. 为了让您动态设置el 3 methods 3种方法

  1. assign value of el in the initializer a valid html string, or selector string or just a valid jQuery DOM object. 在初始化程序中为el值分配一个有效的html字符串,选择器字符串或一个有效的jQuery DOM对象。

  2. assign className or id attributes to divIdentification in the initializer to allow Backbone view to create its own el 在初始化程序中将classNameid属性分配给divIdentification,以允许Backbone视图创建自己的el

  3. Another method is to use setElement here 另一种方法是在这里使用setElement

Your problem is where your create your divIdentification the keyword this in not pointing o your view, try this : 您的问题是您在创建divIdentification关键字this时未指向您的视图,请尝试以下操作:

var roomOccupancyView = new Bookings.RoomOccupancyView({
                    model: datas,
                    el: function() {
                    return ".data-identification-" + 
                        this.model.dataTypeId + 
                        "-" + this.model.dataGrouping.get("data1Id") +
                        "-"+this.model.dataGrouping.get("data2Id");
                    } 
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM