如何JSON添加到骨干,JS收集使用取 [英] how to add json to backbone,js collection using fetch

查看:112
本文介绍了如何JSON添加到骨干,JS收集使用取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让Backbone.js的加载JSON。
JSON的负载,但我不知道如何获取项目到我的收藏。
或者,也许是自动发生,我只是不能描绘出。范围问题?

// JS code

  //模型
VAR的客户= Backbone.Model.extend({
    默认值:{
        名称:'耐克',
        IMG:http://www.rcolepeterson.com/cole.jpg
    },
});
//采集
VAR ClientCollection = Backbone.Collection.extend({
    默认值:{
        模式:客户端
    },
    模式:客户端,
    网址:JSON / client.json
});
//视图
VAR theView = Backbone.View.extend({
    初始化:功能(){
        this.collection =新ClientCollection();
        this.collection.bind(复位,this.render,这一点);
        this.collection.bind(变,this.render,这一点);
        this.collection.fetch();
    },
    渲染:功能(){
        警报(测试+ this.collection.toJSON());
    }
});
MyView的VAR =新theView();

// JSON

  {
    项目:
        {
            名:WTBS
            IMG:无图像
        },        {
            名:XYC
            IMG:无图像
        }
    ]
}


解决方案

您JSON是不正确的格式,你可以修复JSON或暗示的解析方法添加到骨干网:

  VAR ClientCollection = Backbone.Collection.extend({
    默认值:{
        模式:客户端
    },
    模式:客户端,
    网址:JSON / client.json',    解析:功能(响应){
       返回response.items;
    }
});

或修复您的JSON:

  [
        {
            名:WTBS
            IMG:无图像
        },        {
            名:XYC
            IMG:无图像
        }
    ]

I am trying to get backbone.js to load json. The json loads but i am not sure how to get the items into my collection. Or maybe that happens automatically and i just can't trace out. scope issue?

//js code

//model
var Client = Backbone.Model.extend({
    defaults: {
        name: 'nike',
        img: "http://www.rcolepeterson.com/cole.jpg"
    },
});
//collection
var ClientCollection = Backbone.Collection.extend({
    defaults: {
        model: Client
    },
    model: Client,
    url: 'json/client.json'
});
//view
var theView = Backbone.View.extend({
    initialize: function () {
        this.collection = new ClientCollection();
        this.collection.bind("reset", this.render, this);
        this.collection.bind("change", this.render, this);
        this.collection.fetch();
    },
    render: function () {
        alert("test" + this.collection.toJSON());
    }
});
var myView = new theView();

//json

{
    "items": [
        {
            "name": "WTBS",
            "img": "no image"
        },

        {
            "name": "XYC",
            "img": "no image"
        }
    ]
}

解决方案

Your json is not in the correct format, you can fix the json or add a hint to backbone in the parse method:

var ClientCollection = Backbone.Collection.extend({
    defaults: {
        model: Client
    },
    model: Client,
    url: 'json/client.json',

    parse: function(response){
       return response.items;
    }
});

Or fix your JSON:

 [
        {
            "name": "WTBS",
            "img": "no image"
        },

        {
            "name": "XYC",
            "img": "no image"
        }
    ]

这篇关于如何JSON添加到骨干,JS收集使用取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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