到一个给定的object_id抓取并呈现集合的最佳方式 [英] The best way to fetch and render a collection for a given object_id

查看:166
本文介绍了到一个给定的object_id抓取并呈现集合的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个简单的应用程序,它是能够得到一个集合对于一个给定 OBJECT_ID 。结果
GET响应从服务器看起来是这样的:

  [
    {OBJECT_ID:1,内容:MSG1},
    {OBJECT_ID:1,内容:MSG2},
    {OBJECT_ID:1,内容:第三型},
    .......
]

我的目标是:结果
 渲染集合,当用户选择一个 OBJECT_ID

我的code的出发点是:

  this.options = {OBJECT_ID:1};
MyView的=新NeView(_扩展({EL:该$(#MyView的)},this.options));

我的问题是:结果
*什么是最好的方法:结果
1)设置的object_id值<​​/ code>在为MyModel 以结果
2)触发 MyCollection中,然后结果
3)触发渲染 MyView的功能*?
或到活动我的目标是什么?


P.S:

我的基本code是这样的:

  //我查看
定义([
    JS /收藏/ MyCollection的
    JS /模型/ myFeed
],功能(MyCollection的,为MyModel){
    VAR MyView的= Backbone.View.extend({        初始化:功能(){            VAR基于myModel =新为MyModel();            _.bindAll(这一点,渲染);            myModel.set({
                OBJECT_ID:this.options.object_id
            }); //这里我得到一个错误:未捕获类型错误:目标函数(){a.apply(这一点,参数)}没有办法设定
        }
    });    返回MyView的;
});


  // MyCollection的
定义([
    JS /模型/基于myModel
],功能(为MyModel){    VAR MyCollection的= Backbone.Collection.extend({        网址:函数(){
            回归的http://本地主机/电影/+ myModel.get(OBJECT_ID);
        }
    });    返回新MyCollection的
});


  //为MyModel
定义([
]功能(){    VAR为MyModel = Backbone.Model.extend({
    });    返回为MyModel
});


解决方案

有几个,如果不从根本的东西不对您的骨干内部的基本认识。

首先,定义你的默认模式idAttribute,这是你的钥匙,你在集合中查找一个模型什么标识

  //为MyModel
定义([
]功能(){    VAR为MyModel = Backbone.MyModel.extend({
        idAttribute:OBJECT_ID
    });    返回为MyModel
});

您的收藏中,有没有必要在你所定义的方式来定义您的网址,有两件事情需要改变,首先是确定您的收藏的默认模式,第二个是刚刚与底座贴网址为您收集

  // MyCollection的
定义([
    JS /模型/基于myModel
],功能(为MyModel){    VAR MyCollection的= Backbone.MyCollection.extend({
        型号:为MyModel,//添加此
        网址:函数(){
            返回的http://本地主机/电影
        }
    });    返回MyCollection的//不创建一个新的集合,只返回对象
});

,然后你的看法可能是沿着这些线路的东西,但肯定不是限于实现这种方式

  //我查看
定义([
    JS /收藏/ MyCollection的
    JS /模型/ myFeed
],功能(MyCollection的,为MyModel){
    VAR MyView的= Backbone.View.extend({        标签名:UL,        初始化:功能(){
            this.collection =新MyCollection的();
            this.collection.on(添加,this.onAddOne,这一点);
            this.collection.on(复位,this.onAddAll,这一点);
        },        onAddAll:功能(收集,期权)
        {
            collection.each(功能(模型,指数){
                that.onAddOne(模型,集合);
            });
        },        onAddOne:功能(模型,收集,期权)
        {
            //这里呈现出一个单独的模型,或者使用其他骨干观点或纯文本
            这$ el.append('&LT;立GT;'+ model.get('文本')+'&LT; /李&GT;');
        }    });    返回MyView的;
});

别紧张,并走一步看一步

我强烈推荐在上Backbone.js的GitHub的维基教程的详尽列表左看右看:的https://github.com/documentcloud/backbone/wiki/Tutorials%2C-blog-posts-and-example-sites ...尝试加入require.js的额外的复杂性之前了解主干的基本知识

I am trying to implement a simple app which is able to get a collection for a given object_id.
The GET response from the server looks like this:

[
    {object_id: 1, text: "msg1"}, 
    {object_id: 1, text: "msg2"}, 
    {object_id: 1, text: "msg3"},
    .......
]

My goal is:
render a collection when the user choose an object_id.

The starting point of my code is the following:

this.options = {object_id: 1};
myView = new NeView(_.extend( {el:this.$("#myView")} , this.options));

My question is:
* What is the best way:
1) to set the object_id value in the MyModel in order to
2) trigger the fetch in MyCollection and then
3) trigger the render function in myView?*
or to active my goal?


P.S:

My basic code looks like this:

// My View
define([
    "js/collections/myCollection",
    "js/models/myFeed"
], function (myCollection, MyModel) {
    var MyView = Backbone.View.extend({

        initialize: function () {

            var myModel = new MyModel();

            _.bindAll(this, "render");

            myModel.set({
                object_id: this.options.object_id
            }); // here I get an error: Uncaught TypeError: Object function (){a.apply(this,arguments)} has no method 'set'
        }
    });

    return MyView;
});


// MyCollection
define([
    "js/models/myModel"
], function (MyModel) {

    var MyCollection = Backbone.Collection.extend({

        url: function () {
            return "http://localhost/movies/" + myModel.get("object_id");
        }
    });

    return new MyCollection
});


//MyModel
define([
], function () {

    var MyModel = Backbone.Model.extend({
    });

    return MyModel
});

解决方案

There's a few, if not fundamentally things wrong with your basic understanding of Backbone's internals.

First off, define your default model idAttribute, this is what identifies your key you lookup a model with in a collection

//MyModel
define([
], function () {

    var MyModel = Backbone.MyModel.extend({
        idAttribute: 'object_id'
    });

    return MyModel
});

in your collection, there is no need to define your URL in the way you defined it, there are two things you need to change, first is to define the default model for your collection and second is to just stick with the base url for your collection

// MyCollection
define([
    "js/models/myModel"
], function (MyModel) {

    var MyCollection = Backbone.MyCollection.extend({
        model: MyModel, // add this
        url: function () {
            return "http://localhost/movies
        }
    });

    return MyCollection // don't create a new collection, just return the object
});

and then your view could be something along these lines, but is certainly not limited to this way of implementing

// My View
define([
    "js/collections/myCollection",
    "js/models/myFeed"
], function (MyCollection, MyModel) {
    var MyView = Backbone.View.extend({

        tagName: 'ul',

        initialize: function () {
            this.collection = new MyCollection();
            this.collection.on('add', this.onAddOne, this);
            this.collection.on('reset', this.onAddAll, this);
        },

        onAddAll: function (collection, options)
        {
            collection.each(function (model, index) {
                that.onAddOne(model, collection);
            });
        },

        onAddOne: function (model, collection, options)
        {
            // render out an individual model here, either using another Backbone view or plain text
            this.$el.append('<li>' + model.get('text') + '</li>');
        }

    });

    return MyView;
});

Take it easy and go step by step

I would strongly recommend taking a closer look at the exhaustive list of tutorials on the Backbone.js github wiki: https://github.com/documentcloud/backbone/wiki/Tutorials%2C-blog-posts-and-example-sites ... try to understand the basics of Backbone before adding the additional complexity of require.js

这篇关于到一个给定的object_id抓取并呈现集合的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆