来自外部JSON文件的主干集合未呈现

康达西

新秀的问题,但我已经被困了23天了。下面是我的代码,用于显示来自外部阵列的项目列表。我的收藏没有呈现,在控制台中运行“ stations”时可以看到收藏中的项目。

window.App = {
    Views: {},
    Models: {},
    Collections: {}
}

window.template = function(id){
    return _.template( $('#' + id).html() );
};


App.Models.Station = Backbone.Model.extend({
    defaults: {
        name: 'Station',
        bikes: 20
    }
});

App.Collections.Stations = Backbone.Collection.extend({
    model: App.Models.Station,
    url: 'http://api.citybik.es/dublinbikes.json',
    parse : function(response){
    return response;  
    }
});

App.Views.Station = Backbone.View.extend({
    tagName: 'li',

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

    render: function(){
        this.$el.html( this.model.get('name') + ': ' + this.model.get('bikes') + ' bikes available');
        return this;
    }
});

App.Views.Stations = Backbone.View.extend({
    tagName: 'ul',

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

    render: function(){
        this.collection.each(this.addOne, this);
    },

    addOne: function(station){
        var stationView = new App.Views.Station({ model: station });
        this.$el.append(stationView.render().el);
    }
}); 

var stations = new App.Collections.Stations();
stations.fetch();

var stationsView = new App.Views.Stations({ collection: stations });
$('body').prepend(stationsView.$el);
海龟

我认为jack是对的-这样的事情可能对您有用:

var stations = new App.Collections.Stations();
stations.fetch({success: function(){
  var stationsView = new App.Views.Stations({ collection: stations });
  $('body').prepend(stationsView.$el);
}});

从返回的jqxhr对象使用延迟的APIfetch

var stations = new App.Collections.Stations();
var stationsLoaded = stations.fetch();

stationsLoaded.done(function(){
  var stationsView = new App.Views.Stations({ collection: stations });
  $('body').prepend(stationsView.$el);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

主干从外部json填充集合

来自分类Dev

.htaccess拒绝来自外部请求的访问

来自分类Dev

仅允许来自外部网络的api

来自分类Dev

来自外部文件的变量[已解决]

来自分类Dev

Eclipse不显示来自外部JAR文件的类

来自分类Dev

Jekyll包含来自外部文件的变量

来自分类Dev

来自外部文件的画布上的Android位图

来自分类Dev

来自外部来源的缩略图未显示在随机帖子小部件中

来自分类Dev

是否可以阻止来自外部的请求?

来自分类Dev

来自外部库的Swift函数未导入

来自分类Dev

来自外部文件夹的参考代码文件

来自分类Dev

禁止来自外部功能的错误消息

来自分类Dev

来自外部文件的C#.NET运行代码

来自分类Dev

来自外部XIB的DidSet崩溃

来自分类Dev

IONIC / Angular不显示来自外部API的JSON结果

来自分类Dev

来自外部硬盘的Ubuntu

来自分类Dev

来自外部文件JavaScript的未捕获的ReferenceError

来自分类Dev

来自外部js的函数

来自分类Dev

来自AMD项目中json文件的主干绑定数据

来自分类Dev

按钮上的主干视图未呈现

来自分类Dev

Jekyll包含来自外部文件的变量

来自分类Dev

来自外部javascript文件的getElementById

来自分类Dev

流星:来自外部API调用的数据未呈现

来自分类Dev

解析来自外部动态加载的js文件的信息

来自分类Dev

来自外部集群的NMI

来自分类Dev

角度搜索来自外部JSON对象的值

来自分类Dev

Ammap 线模拟来自外部 JSON 文件

来自分类Dev

来自外部文件的木偶迭代

来自分类Dev

来自外部 js 文件的 D3 SVG