Backbone.js el 不工作

发布于 2024-10-30 03:39:53 字数 1172 浏览 1 评论 0 原文

App.Views.VideoView = Backbone.View.extend({
    initialize: function() {
        _.bindAll(this, 'render');
        this.model = this.options.model;
        this.render();
    },
    render: function() {
        JST.video({
            model: this.model
        });
        return this;
    }
});

App.Views.PlayListView = Backbone.View.extend({
    el: $("#playlistWrapper"),
    initialize: function(videos) {
        _.bindAll(this, 'render');
        this.modelViews = $.map(videos.models, function(model, i) {
            return new App.Views.VideoView({
                model: model
            });
        })
        this.render();

    },
    render: function() {
        var that = this;
        $(this.el).clear();
        $.each(this.modelViews, function(i, modelView) {
            $(that).el.append(modelView.render().el);
        });

        return this;
    }
});

我总是遇到以下错误

$(this.el).clear is not a function
[Break On This Error] $(this.el).clear(); 

,似乎我的 PlayerListView 是空的。

我有一个带有 id playlistWrapper 的 div。

如果我对 playlistWrapper 使用 jquery 选择器,它会给出正确的元素。

我做错了什么。

App.Views.VideoView = Backbone.View.extend({
    initialize: function() {
        _.bindAll(this, 'render');
        this.model = this.options.model;
        this.render();
    },
    render: function() {
        JST.video({
            model: this.model
        });
        return this;
    }
});

App.Views.PlayListView = Backbone.View.extend({
    el: $("#playlistWrapper"),
    initialize: function(videos) {
        _.bindAll(this, 'render');
        this.modelViews = $.map(videos.models, function(model, i) {
            return new App.Views.VideoView({
                model: model
            });
        })
        this.render();

    },
    render: function() {
        var that = this;
        $(this.el).clear();
        $.each(this.modelViews, function(i, modelView) {
            $(that).el.append(modelView.render().el);
        });

        return this;
    }
});

i am always getting below error

$(this.el).clear is not a function
[Break On This Error] $(this.el).clear(); 

it seems my el of PlayerListView is empty.

i have div with id playlistWrapper.

if i use jquery selector for playlistWrapper it gives proper element.

what am i doing wrong.

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

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

发布评论

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

评论(3

回眸一笑 2024-11-06 03:39:53

我在这方面有点晚了,但问题是你在加载 DOM 之前指定了一个 jquery 选择器。

主干对象是使用传递给扩展方法的对象文字来定义的。例如,以下内容在功能上是相同的:

MyView = Backbone.View.extend({
  el: "#foo"
});

var viewObj = {el: "#foo"};
MyView2 = Backbone.View.extend(viewObj);

立即解析并执行对象文字中键/值对右侧的值。这意味着用于 el 的 jQuery 选择器将在您声明它后立即进行解析,而不是在实例化视图时进行解析。很可能,您的应用程序中包含了 javascript 文件,并且在加载 DOM 之前下载了该文件,因此 jquery 选择器无法找到您所引用的元素。

您可以采取多种措施来解决此问题。

  • 每当您需要使用
  • 可以在视图初始值设定项中设置 this.el
  • 元素时,您都可以调用 $(this.el)您可以设置 {el: $ ("#whatever")} 作为视图构造函数的参数,假设视图是在 DOM 加载后构建的,
  • 您可以使用 javascript 模块模式将视图和其他主干对象的定义推迟到 DOM 之后已加载
  • ,可能还有一些我目前没有想到的其他选项

I'm a little late to the party on this, but the problem is that you're specifying a jquery selector before the DOM is loaded.

A backbone object is defined with an object literal passed in to the extend method. For example, the following are functionally the same:

MyView = Backbone.View.extend({
  el: "#foo"
});

var viewObj = {el: "#foo"};
MyView2 = Backbone.View.extend(viewObj);

values on the right-hand side of a key/value pair in an object literal are parsed and executed immediately. this means that a jQuery selector used for el will be parsed as soon as you declare it, not when the view is instantiated. chances are, you have your javascript file included in your app and it's being downloaded before the DOM is loaded, so the jquery selector can't find the element you're referring to.

There are a number of things you can do to work around this.

  • you can call $(this.el) whenever you need to use the element
  • you can set this.el in the view initializer
  • you can set {el: $("#whatever")} as a parameter to the view constructor, assuming the view is constructed after the DOM has loaded
  • you can use the javascript module pattern to defer definition of the views and other backbone objects until after the DOM is loaded
  • and probably a handful of other options that i'm not thinking of at the moment
巴黎夜雨 2024-11-06 03:39:53

好吧,clear 不是一个 jQuery 函数...您可能正在寻找空?

对您的代码的评论:

在您的视频视图中:

  • 无需从选项中分配模型,这是为您完成的,
  • 您可能希望将模板(JST)的结果附加到 this.el 否则什么都不会显示...

在你的播放列表视图中:

  • 在你的渲染中,在每个循环中,将 $(that).el 更改为 $(that.el)
  • 因为你将 el 定义为 jQuery,所以不需要使用 $(this.el) 和超过

Well clear is not a jQuery function... You might be looking for empty?

Comments on your code:

In you video view:

  • no need to assign the model from the options, this is done for you
  • you might want to append the result of the templating (JST) to this.el otherwise nothing will show up...

In your playlist view:

  • in your render, in your each loop, change $(that).el to $(that.el)
  • since you define el as a jQuery, you do not need to use $(this.el) over and over
猫腻 2024-11-06 03:39:53

使用 this.$el.clear();
并更新 jquery.js 文件,例如

并首先绑定初始化,然后 el:bind。

use this.$el.clear();
and update jquery.js file like

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script>

and first Bind Initialize then el: bind.

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