Backbone.View“el”困惑

发布于 2024-10-31 08:53:47 字数 248 浏览 1 评论 0原文

视图的 el 应该如何处理? 必须设置它,否则事件不会触发(请参阅​​此处)。

但它应该是页面上已有的元素吗? 在我的应用程序中,我将(jQuery 模板)模板渲染到 Fancybox 中。在这种情况下,el 应该是什么?

How should a view's el be handled?
It has to be set, otherwise events don't fire (see here).

But should it be an element that is already on the page?
In my app, I render a (jQuery Templates) template into a Fancybox. What should the el be in that case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兲鉂ぱ嘚淚 2024-11-07 08:53:47

视图 el 是所有事件绑定发生的地方。您不必使用它,但如果您希望主干触发事件,您需要在 el 上进行渲染工作。视图 el 是一个 DOM 元素,但它不必是预先存在的元素。如果您不从当前页面中提取一个,它将被创建,但如果您想看到它执行任何操作,则必须将其插入页面中。

一个例子:
我有一个创建单个项目的视图

window.ItemView = Backbone.View.extend({
    tagName: "li", //this defaults to div if you don't declare it.
    template: _.template("<p><%= someModelKey %></p>"),
    events: {
         //this event will be attached to the model elements in
         //the el of every view inserted by AppView below
        "click": "someFunctionThatDoesSomething"
    },
    initialize: function () { 
        _.bindAll(this, "render");
        this.render();
    },
    render: function () {
        this.el.innerHTML = this.template(this.model.toJSON());
        return this;
    }
});
window.AppView = Backbone.View.extend({
    el: $("#someElementID"), //Here we actually grab a pre-existing element
    initialize: function () { 
        _.bindAll(this, "render");
        this.render(new myModel());
    },
    render: function (item) { 
        var view = new ItemView({ model: item });
        this.el.append(view.render().el);
    }
});

第一个视图仅创建列表项,第二个视图实际上将它们放置在页面上。我认为这与 Backbone.js 网站上的 ToDo 示例 中发生的情况非常相似。我认为约定是将您的内容呈现到 el 中。因此,el 可以作为放置模板内容的着陆点或容器。然后,Backbone 将其事件绑定到其内部的模型数据。

创建视图时,您可以使用 el:tagName:className:id 以四种方式操作 el:。如果这些都没有声明,el 默认为没有 id 或 class 的 div。此时它也未与该页面关联。您可以使用 tagName 将标签更改为其他内容(例如 tagName: "li",将为您提供

  • 的 el) 。同样可以设置el的id和class。 el 仍然不是您页面的一部分。 el 属性允许您对 el 对象进行非常精细的操作。大多数时候我使用 el: $("someElementInThePage") 它实际上将您在视图中对 el 所做的所有操作绑定到当前页面。否则,如果您希望看到在视图中完成的所有艰苦工作都显示在页面上,则需要将其插入/附加到页面视图中的其他位置(可能在渲染中)。我喜欢将 el 视为所有视图都可以操作的容器。

    A views el is where all the event binding takes place. You don't have to use it but if you want backbone to fire events you need to do your rendering work on the el. A views el is a DOM element but it does not have to be a pre-existing element. It will be created if you do not pull one from your current page, but you will have to insert it into the page if you ever want to see it do anything.

    An example:
    I have a view that creates individual items

    window.ItemView = Backbone.View.extend({
        tagName: "li", //this defaults to div if you don't declare it.
        template: _.template("<p><%= someModelKey %></p>"),
        events: {
             //this event will be attached to the model elements in
             //the el of every view inserted by AppView below
            "click": "someFunctionThatDoesSomething"
        },
        initialize: function () { 
            _.bindAll(this, "render");
            this.render();
        },
        render: function () {
            this.el.innerHTML = this.template(this.model.toJSON());
            return this;
        }
    });
    window.AppView = Backbone.View.extend({
        el: $("#someElementID"), //Here we actually grab a pre-existing element
        initialize: function () { 
            _.bindAll(this, "render");
            this.render(new myModel());
        },
        render: function (item) { 
            var view = new ItemView({ model: item });
            this.el.append(view.render().el);
        }
    });
    

    The first view just creates the list items and the second view actually places them on the page. I think this is pretty similar to what happens in the ToDo example on the backbone.js site. I think convention is to render you content into the el. So the el serves as a landing place or a container for placing your templated content. Backbone then binds its events to the model data inside of it.

    When you create a view you can manipulate the el in four ways using el:, tagName:, className:, and id:. If none of these are declared el defaults to a div without id or class. It is also not associated with the page at this point. You can change the tag to something else by using using tagName (e.g. tagName: "li", will give you an el of <li></li>). You can set the id and class of el likewise. Still el is not a part of your page. The el property allows you to do very fine grain manipulation of the el object. Most of the time I use an el: $("someElementInThePage") which actually binds all the manipulation you do to el in your view to the current page. Otherwise if you want to see all the hard work you have done in your view show up on the page you will need to insert/append it to the page somewhere else in your view (probably in render). I like to think of el as the container that all your view manipulates.

    德意的啸 2024-11-07 08:53:47

    现在有点老了,但我也很困惑,所以对于其他到达这里的人来说,这个小提琴可能会有所帮助 - http://jsfiddle。净/hRndn/2/

    var MyView = Backbone.View.extend({
    
        events: {
            "click .btn" : "sayHello",
        },
    
        sayHello : function() {
            alert("Hello");
        },
    
    
        render : function() {
            this.$el.html("<input type='button' class='btn' value='Say Hello'></input>");
    
        }
    });
    
    $(function() {
        myView = new MyView({el:"#parent_id"});
        myView.render();
    });
    

    Bit old now, but I was confused as well, and so for other people that get here, this fiddle might help - http://jsfiddle.net/hRndn/2/

    var MyView = Backbone.View.extend({
    
        events: {
            "click .btn" : "sayHello",
        },
    
        sayHello : function() {
            alert("Hello");
        },
    
    
        render : function() {
            this.$el.html("<input type='button' class='btn' value='Say Hello'></input>");
    
        }
    });
    
    $(function() {
        myView = new MyView({el:"#parent_id"});
        myView.render();
    });
    
    城歌 2024-11-07 08:53:47

    您希望“el”引用一个包含子元素的元素,该子元素具有触发视图更改的任何事件。可以与“body”标签一样宽。

    You want your 'el' to reference an element that contains a child element that has any event that triggers a change in your view. Could be as wide as a "body" tag.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文