我的'标识'动态加载到我的收藏骨干? [英] Dynamically load my 'IDs' into my Backbone Collection?

查看:96
本文介绍了我的'标识'动态加载到我的收藏骨干?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有这方面的工作与另一个问题张贴在这里的答案,但我试图做的东西多一点。我有一个主干设置如下,

Ok, I have got this working with answers posted on another question here, but I am trying to do something a little more. I have a Backbone set up as follows,

var MyModel= Backbone.Model.extend();

var MyCol = Backbone.Collection.extend({
model : MyModel,
url: '/GetData/2',
parse: function(response) {
  return response;
    }
});

var stuff = new MyCol;

stuff.fetch({
   success: function (collection, response) {
        console.log(response);
   }
})

现在这个code完全有效。现在来解释,在网​​址设置为 PHP修身,返回 JSON EN codeD数据,其中将有四个键/对分组,例如:

Now this code fully works. Now to explain, the URL is set with PHP Slim, which returns JSON encoded data, where will be four key/pair groupings, for example:

<code> { id; XX, data: XX, another:XX, last:YY } </code>

这正在接收数据形成数据库,现在我应该怎么做动态?我的意思是,关于网​​址我的收藏线,它通过 2 电流ID 。我怎样才能获得骨干致电每个 ID我需要的,目前有 6 在数据库中上市。所以,我需要它显示/控制台日志(目前用于测试)所有的 6 输入,因此我将如何改变这种状况 ID 输入 1-6 ?我应该只需要使用的洗手间?我真的想要的东西,并不需要改变,如果说名单表数据库中删除以上的添加?

This data is being received form a database, now how should I do this dynamically? By that I mean that on the URL' line in my collection, it passes 2 as the current ID. How can I get backbone to call each ID I need, there are currently 6 listing in the database. So I need it display/console log (currently for testing) all 6 inputs, so how would I change that ID input to 1-6? Should I just use a for loo? I really want something that will not need to change if say lists are removed form the database or more are added?

最终目的是为这个 JSON数据是它加载到我的骨干视图,这是一个模板格式

The end aim is for this JSON data is for it to load into my Backbone view, which is a template for a form.

所有帮助最欢迎的,

感谢

格伦

P.S很抱歉,如果我的拼写是关闭的,我诵读困难的,也可能没有解释正确的事情,所以让我知道,我会提高我的措辞,谢谢。

P.S Sorry if my spelling is off I am dyslexic, also may not have explained things right, so let me know and I will improve my wording, thanks.

EDITS

这就是我与现在的工作:

This is what I am working with now ::

 var AdminModel = Backbone.Model.extend({
   defaults: {},
   urlRoot: '/GetData'

 });

 var AdminColModel = Backbone.Collection.extend({
model : AdminModel,
url: '/',
parse: function(response) {
  //console.log(response);
  return response;
    }
});

 var stuff = new AdminColModel;

  stuff.fetch({
    success: function (collection, response) {
     console.log(collection);
   } 
 })

这并不控制台登录了什么呢?但PHP设置和工作正常,所以当你去 /的getData 你会得到一个完整列表,如果在数据库中的所有行。当您查看/的getData / X&LT; - X是ID号,这将只是返回的行ID数据

This does not console log anything at all? But the PHP is set up and working fine, so when you go to /getData you get a whole list if all the rows in the database. When you view /getData/X <- X is the id number, that will return just that rows ID data.

格伦

推荐答案

您不应该让在集合级别的集合中的每个型号的个人请求。你已经为你的收藏的网址应该是默认请求所有的车型。因此,而不是它是:

You should not make an individual request for each model in your collection at the collection level. The url you have set for your collection should request all of the models by default. So instead of it being:

 '/GetData/2'

这应该不是是:

 '/GetData'

然后在服务器端,您应该确保的GetData 操作返回所有 MyModels

您就可以写的GetData 接受参数要么返回一个单独的模型,或者筛选结果覆盖。你会在个别型号检索,这应该是在模型层使用的URL特别感兴趣。事实上,你已经有这个动作,因为你正在使用它(错误地)来检索集合。这与 /的GetData / 2 URL相关联的操作。这个动作应该当你调用模型中提取,是一惯的集合。

You can then write overrides for GetData that take parameters to either return an individual model, or filter the results. You will be particularly interested in the individual model retrieval, which should be the URL used at the model level. in fact, you already have this action, as you are using it (incorrectly) to retrieve the collection. This is the action associated with the /GetData/2 URL. This action should be the one used when you call fetch inside the model, not the collection.

希望这有助于。

后修改

添加了一些code协助。

Adding some code to assist.

var AdminModel = Backbone.Model.extend({
    urlRoot: '/GetData'
});

var AdminCollection = Backbone.Collection.extend({
    model : AdminModel,
    url: '/GetData'
});

var wholeCollection= new AdminCollection();

// the fetch method on the collection uses the AdminCollection.url property
wholeCollection.fetch({
    success: function (collection) {
        console.log(collection); // will output collection object to console
    } 
});

// this model does not exist in any collection
var modelOutsideCollection = new AdminModel({}, { id: "2" });

// because the model is not in a collection, fetch uses the AdminModel.urlRoot proeprty
modelOutsideCollection.fetch({
    success: function (model) {
        console.log(model); // outputs a single model with id of 2 to collection
    } 
}); 

这篇关于我的'标识'动态加载到我的收藏骨干?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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