为什么取()不起作用? [英] Why fetch() does not work?

查看:101
本文介绍了为什么取()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个JSON网址提取的集合。骨干确实发送请求并不会得到响应之后却没有模式集合中的:

下面是我的JavaScript:

  stores.fetch();

JSON在响应

  [{名:存储1},{名:存储2},{名:存储3},{名 :存储4}]

在响应中的Content-Type HTTP头是应用程序/ JSON

为什么不将其加载到集合?是JSON是否正确?

一些较code:

  be.storeList.Item = Backbone.Model.extend({
    默认值:{
        ID:空,
        名称:空,
        描述:空
    },
    初始化:功能(ATTRS){
        attrs.id = this.cid;
        this.set(ATTRS);
    }
});be.storeList.Items = Backbone.Collection.extend({
    型号:be.storeList.Item,
    网址:'/管理/存储
});VAR店=新be.storeList.Items();
stores.fetch();
的console.log(stores.toJSON());


解决方案

是异步的。尝试

  stores.fetch({
    成功:函数(){
        的console.log(stores.toJSON());
    }
});

  stores.on(同步,函数(){
    的console.log(stores.toJSON());
});
stores.fetch();

  stores.fetch()。然后(函数(){
    的console.log(stores.toJSON());
});

I'm trying to fetch a collection from a JSON url. The backbone does send the request and does get a response but there are no models in the collection after it:

Here is my JavaScript:

stores.fetch();

JSON in the response

[{"name":"Store 1"},{"name":"Store 2"},{"name":"Store 3"},{"name":"Store 4"}]

The Content-Type HTTP header in the response is application/json.

Why doesn't it load into the collection? Is the JSON correct?

Some more code:

be.storeList.Item = Backbone.Model.extend({
    defaults: {
        id: null,
        name: null,
        description: null
    },
    initialize:function(attrs){
        attrs.id = this.cid;
        this.set(attrs);
    }
});

be.storeList.Items = Backbone.Collection.extend({
    model: be.storeList.Item,
    url:'/admin/stores'
});

var stores = new be.storeList.Items();
stores.fetch();
console.log(stores.toJSON());

解决方案

fetchis asynchronous. Try

stores.fetch({ 
    success:function() {
        console.log(stores.toJSON());
    }
});

or

stores.on("sync", function() {
    console.log(stores.toJSON());
});
stores.fetch();

or

stores.fetch().then(function() {
    console.log(stores.toJSON());
});

这篇关于为什么取()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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