简体   繁体   English

通过ID从骨干js的集合中获取模型

[英]Get a model by ID from a collection in backbone js

I'm trying to figure it out how to get a model by it's id and display the same to view. 我试图弄清楚如何通过其ID获取模型并显示该模型以进行查看。 I have setup my router to get the data by id. 我已设置路由器以按ID获取数据。 I tried a lot but unable to find a solution. 我尝试了很多,但是找不到解决方案。 Any idea on this would be appreciated. 任何想法,将不胜感激。

collection.js file: collection.js文件:

app.Collections.qualityCollection = Backbone.Collection.extend({
    model: app.Models.qualityModel,
    url: "http://localhost/socialapp/php/fetch.php"
});

var userCollections = new app.Collections.qualityCollection();

userCollections.fetch();

router.js file: router.js文件:

app.Routers.socialRouter = Backbone.Router.extend({
        routes: {
            "" : "homePage",
            "make_friends/:id" : "userProfile",
        },

        userProfile: function() {
            new app.Views.idView({ model: userCollections.get(id) });
        }

    });

    new app.Routers.socialRouter();

    Backbone.history.start();

view.js file: view.js文件:

app.Views.idView = Backbone.View.extend({
    el: $("#app"),

    template: _.template($('#public-profile').html()),

    initialize: function(){
        this.render();
    },

    render: function () {
        console.log(this.model); /// undefined
        this.$el.html(this.template( this.model.toJSON() )); 
        return this;
    }
    });

JSON Data from the URL: [ { "id": "1", "firstname": "Abc", "lastname": "Xyz" }, { "id": "2", "firstname": "Klm", "lastname": "Opq" } ] 来自URL的JSON数据: [ { "id": "1", "firstname": "Abc", "lastname": "Xyz" }, { "id": "2", "firstname": "Klm", "lastname": "Opq" } ]

Expected Router Link: http://localhost/socialapp/#/make_friends/2 预期的路由器链接: http:// localhost / socialapp /#/ make_friends / 2

To answer your question, you receive the value of route param in the callback. 要回答您的问题,您将在回调中收到route param的值。 You can just pass it to the view and access it in it's initialize 您可以将其传递给视图并在初始化时访问它

app.Routers.socialRouter = Backbone.Router.extend({
    routes: {
        "" : "homePage",
        "make_friends/:id" : "userProfile",
    },

    userProfile: function(id) {
        // no reference?
        new app.Views.idView({ collection: userCollections, modelId: id });
    }

});

If you're view is actually an item view, you don't need to pass the entire collection, you can just do the following after fetching the collection in router. 如果您的视图实际上是项目视图,则无需传递整个集合,只需在router中获取集合后即可执行以下操作。

new app.Views.idView({ model: userCollections.get(id) });

Better yet, your view don't need a collection, you just need a model instance with an end point that returns a model's info 更好的是,您的视图不需要集合,只需要一个带有返回模型信息的端点的模型实例

userCollections.get(id) should work. userCollections.get(id)应该可以工作。

Considering Id is attribute of the model, you can find the model as below too. 考虑到Id是模型的属性,您也可以如下找到模型。

userCollections.find(function(model) { return model.get('id') === Id; }); userCollections.find(function(model){return model.get('id')=== ID;});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM