使用fetch处理来自backbone.js集合的响应

时间:2012-10-20 20:40:09

标签: backbone.js

我对backbone.js很新,并且最近管理完成了我的第一个应用程序。我创建了一个负责通过API获取数据的集合,但是无法遍历结果并使用它。

这是我的模型文件

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

    var VehicleLookupModel = Backbone.Model.extend({
        //data will contain one of the items returned from the collection's 'parse' function.
        parse: function(data){
            return data;
        }
    })

    return VehicleLookupModel;

});

收集档案

define([
    'jquery',
    'underscore',
    'backbone',
    'l/models/VehicleLookupModel'
], function($, _, Backbone, VehicleLookupModel){

    var VehicleLookupModelSet = Backbone.Collection.extend({
        model   : VehicleLookupModel,

        url     : function() {
            return '/en/car/api/model-lookup-model.json/'+this.make+'/';
        },

        parse : function(response) {
            return response;
        },

        initialize: function(options) {
            options || (options = {});
            this.make = options.make;
        }
    })

    return VehicleLookupModelSet;

});

最后是视图文件

define([
    'jquery',
    'underscore',
    'backbone',
    'l/collections/VehicleLookupMakeSet',
    'l/collections/VehicleLookupModelSet',
    'l/collections/VehicleLookupTrimSet'
], function($, _, Backbone, VehicleLookupMakeSet, VehicleLookupModelSet, VehicleLookupTrimSet){

    var BrowseVehicleView = Backbone.View.extend({
        el: $('#vehicle-browse-form'),
        initialize: function(){

            // Extend JQuery example
            // This would extend JQuery function for resetting elements on the form
            $.fn.extend({
                resetElement: function(){
                    $(this).attr('disabled', 'disabled');
                    $(this).html('');

                    return $(this);
                }
            });

            // define array of elements to be used in DOM manipulations
            this.elements = {
                "make"      : $('#id_make',     this.el),
                "model"     : $('#id_model',    this.el),
                "trim"      : $('#id_trim',     this.el),
                "year"      : $('#id_year',     this.el)
            }

        },

        events: {
            "change #id_make"   : "onMakeChange",
            "change #id_model"  : "onModelChange",
            "change #id_trim"   : "onTrimChange"
        },
        render: function(){
            // Using Underscore we can compile our template with data

        },
        onMakeChange: function(event) {
            this.elements.model.resetElement();
            this.elements.trim.resetElement();
            this.collection = new VehicleLookupModelSet({make: this.elements.make.val()})
            this.collection.fetch();

            console.log(this.collection);



        },
        onModelChange: function(event) {
            var VehicleLookupTrimInstance = new VehicleLookupTrimSet({make: this.elements.make.val(), model: this.elements.model.val()})
            VehicleLookupTrimInstance.fetch();

        },
        onTrimChange: function(event) {

        },
        renderItem:  function(object, item) {
            console.log(item);
        }

    });
    // Our module now returns our view
    return new BrowseVehicleView;
});

以上是console.log(this.collection)返回一个具有许多属性的对象,但不确定如何使用。但是,我注意到有一个方法“模型”,并且在模型内部有许多对象,每个对象代表json的值。

任何想法我如何循环对象?

1 个答案:

答案 0 :(得分:8)

this.collection.fetch({
  success: function(collection, response) {
    _.each(collection.models, function(model) {
      console.log(model.toJSON());
    })
  }
});