在 Backbone.js 的 View 中获取 JSON 数据

发布于 2024-12-22 17:09:50 字数 1716 浏览 1 评论 0 原文

这是我的代码。请随意告诉我我做错了什么或做对了什么。我试图将所有内容存储在 PhishVids 对象中。如果这是错误的做法,请告诉我。

我的 JSON 存储在 /shows/YEAR.json 中。年份范围为 1987 年至 2011 年。我似乎无法加载 JSON,所以有人可以指出我正确的方向吗?

var PhishVids = {
    Models: {
        Show: Backbone.Model.extend({
            defaults: {
                showid: 'show id',
                year: 'year',
                month: 'month',
                day: 'day',
                venue: 'venue'
            }
        })
    },

    Views: {
        Show: Backbone.View.extend({
            el: $('#content'),
            initialize: function() {
                this.model.fetch();
                this.model.bind('change', this.render, this);
            },
            render: function(event) {
                var compiled_template = _.template($("#shows-template").html());
                this.el.html(compiled_template(this.model.toJSON()));
                return this; //recommended as this enables calls to be chained.
            },
            events: {
                "click .show": "showClick"
            },
            showClick: function(event) {

            }
        })
    },

    Collections: {
        ShowList: Backbone.Collection.extend({
            parse: function(response) {
                return response.items;
            }
        })
    }

};

显示模板:

<script type="text/template" id="shows_template">
    <a href='/<%= year %>/<%= month %>/<%= day %>' class='show <%= month %> <%= day %>' id='<%= showid %>'><%= month %>/<%= day %></a>
    <div class='venue'><%= venue %></div>
</script>

Here's my code. Feel free to tell me what I'm doing wrong or right. I'm trying to store everything in the PhishVids object. If this is the wrong way to do things, please let me know.

My JSON is stored in /shows/YEAR.json. The years range from 1987-2011. I can't seem to get the JSON loaded in, so can anyone point me in the right direction?

var PhishVids = {
    Models: {
        Show: Backbone.Model.extend({
            defaults: {
                showid: 'show id',
                year: 'year',
                month: 'month',
                day: 'day',
                venue: 'venue'
            }
        })
    },

    Views: {
        Show: Backbone.View.extend({
            el: $('#content'),
            initialize: function() {
                this.model.fetch();
                this.model.bind('change', this.render, this);
            },
            render: function(event) {
                var compiled_template = _.template($("#shows-template").html());
                this.el.html(compiled_template(this.model.toJSON()));
                return this; //recommended as this enables calls to be chained.
            },
            events: {
                "click .show": "showClick"
            },
            showClick: function(event) {

            }
        })
    },

    Collections: {
        ShowList: Backbone.Collection.extend({
            parse: function(response) {
                return response.items;
            }
        })
    }

};

Show Template:

<script type="text/template" id="shows_template">
    <a href='/<%= year %>/<%= month %>/<%= day %>' class='show <%= month %> <%= day %>' id='<%= showid %>'><%= month %>/<%= day %></a>
    <div class='venue'><%= venue %></div>
</script>

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

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

发布评论

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

评论(1

闻呓 2024-12-29 17:09:50

我在这里可以看到一些问题。如果没有看到调用代码、JSON 示例,并且更多地了解您正在尝试执行的操作,则不可能知道您实际在执行的操作。但是,我立即注意到以下几点:

1)当您获取集合时,该集合将根据它接收到的数据(应该是对象数组)创建模型实例。它将使用 this.model 作为类型,但您尚未定义模型应该是什么.. 2 )

您的模型和集合都没有 urlurlRoot 成员,这将告诉他们如何获取

3) 在您的视图对象中,您的初始化函数(在对象构造完成后立即调用)引用了 this.model,该模型尚未被调用在任何地方设置,因此将是未定义的。

通常,如果您要使用类似的东西,您应该首先实例化集合(new object()),然后调用 .fetch() 来获取数据。 fetch() 可以接受成功和错误的对象作为回调,也可以返回一个 jquery 延迟对象(如果您使用的是 jquery),您可以使用 .done(..) 和 .fail(..) 来找出何时它完成了。

然后,您可以将模型作为选项(对象的一部分)传递到新的 ...View({model: model_you_want}) 等中,并使用它来渲染。

希望有帮助

I can see a few problems here. Without seeing the calling code, an example of your JSON, and knowing a bit more about what you are trying to do it's impossible to know what you are actually doing. However, here are a few things I notice right off:

1) When you fetch a collection, the collection will create model instances out of the data it receives (which should be an array of objects). It will use this.model as the type, but you haven't defined what model should be...

2) Neither your model nor your collection has either url or urlRoot members which would tell them how to fetch

3) In your view object, your initialize function (which is called right after the object is constructed) references this.model, which hasn't been set anywhere and will therefore be undefined

Generally if you're going to use something like this you should first instantiate the collection (new object()) and then call .fetch() to get the data. fetch() can either accept an object with success and error as callbacks or it returns a jquery deferred object (if you're using jquery) that you can use .done(..) and .fail(..) to find out when it finished.

Then you can pass the model in as an option (part of an object) into the new ...View({model: model_you_want}), etc, and use that to render.

Hope that helps

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