简体   繁体   English

Backbone.js-如何从集合中获取值?

[英]Backbone.js - How I can get values from collection?

I am using jquery, backbonejs and underscorejs for my project. 我在我的项目中使用jquery,beltonejs和underscorejs。 My question is, how I can get values from the collection given? 我的问题是,如何从给定的集合中获取价值?

BranchModel BranchModel

define([
  'underscore',
  'backbone'
], function(_, Backbone) {

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

  return BranchModel;

});

BranchCollection BranchCollection

define([
  'jquery',
  'underscore',
  'backbone',
  'models/branch/BranchModel'
], function($, _, Backbone, BranchModel){
  var BranchCollection = Backbone.Collection.extend({
    model: BranchModel,
    url: "data.json",
        parse: function(data) {
            return data;
        }
  });

  return BranchCollection;
});

BranchView BranchView

define(['jquery', 'underscore', 'backbone', 'collections/branch/BranchCollection', 'text!templates/locate-us/mapTemplate.html'], function($, _, Backbone, BranchCollection, mapTemplate) {
    var MapView = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {
            this.$el.off();
        },
        render: function(id) {
            var that = this;
            this.collection = new BranchCollection();
            var formValues = {
                id: id
            };
            this.collection.fetch({
                data: formValues,
                success: function(collection, response) {
                    that.$el.html(mapTemplate);
                },
                error: function() {
                    console.log("error");
                }
            });
        },
    });
    return MapView;
});

data.json data.json

[
    {
      "id": "1",
      "name": "KCP KEBAYORAN LAMA, FMT-JKT",
      "address":"Jl. Raya Kebayoran Lama No. 22 (PAL VII), Kel. Sukabumi Utara - Kec. Kebon Jeruk, Jakarta Barat 11540",
      "latitude":"-6.1773997",
      "longitude":"106.8409396"
    },
    {
      "id": "2",
      "name": "KK JL. PANJANG, PDI-JKT",
      "address":"Jl. Panjang No 37 D, RT 006, RW 011, Kecamatan Kebon Jeruk, Kelurahan Kebon Jeruk, Jakarta Barat",
      "latitude":"-6.1632894",
      "longitude":"106.7633571"
    }
]

Let say, i want to retrieve record id no 1, so I will get this values. 假设我想检索记录ID 1,所以我将获得此值。

              "id": "1",
          "name": "KCP KEBAYORAN LAMA, FMT-JKT",
          "address":"Jl. Raya Kebayoran Lama No. 22 (PAL VII), Kel. Sukabumi Utara - Kec. Kebon Jeruk, Jakarta Barat 11540",
          "latitude":"-6.1773997",
          "longitude":"106.8409396"

Thanks a lot in advance. 非常感谢。

It seems that you need to fetch models by id , for this you need to provide a urlRoot in your model . 似乎您需要通过id来获取模型,为此,您需要在model提供urlRoot For example : 例如 :

var BranchModel = Backbone.Model.extend({
 urlRoot : '/branch'
});

This means that you have to have a urls for branch resource in your Rest API 这意味着您必须在Rest API具有分支资源的网址

/branch        // returns all branches
/branch/:id    // returns a single branch that has this id

And now you can start fetching the model by id as following 现在,您可以开始通过id来获取模型,如下所示

var model = new BranchModel({id:someId});
model.fetch();

Hope this was helpful. 希望这会有所帮助。

You mean this.collection.get("1") ? 您是说this.collection.get("1")吗?

If your collection get the values from server properly, you can get the right thing you want with this code above. 如果您的集合正确地从服务器获取了值,则可以通过上面的代码获得所需的东西。

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

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