Backbone.js的集合不应用模式(使用code点火器) [英] Backbone.js Collections not applying Models (using Code Igniter)

查看:159
本文介绍了Backbone.js的集合不应用模式(使用code点火器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发使用codeIgniter和Backbone.js的一个网站,但我试图模型设置为一个集合我称取()当运行到一个问题。我使用REST API由Phil鲟鱼,以及收集使用时,取()我收到一个JSON响应,但没有孩子的型号将被添加到它。

下面是我使用的JavaScript:

  $(文件)。就绪(函数(){
    window.Person = Backbone.Model.extend({});
    window.People = Backbone.Collection.extend({
        型号:人,
        网址:/ API /内容/用户/格式/ JSON
    });
});

和我的CI控制器:

 要求APPPATH'/库/ REST_Controller.php。一流的内容扩展REST_Controller {
    功能users_get(){
        $用户=阵列(
            阵列('ID'=大于1,'名'=>有些家伙','邮件'= GT;'example1@example.com'),
            阵列('ID'=大于2,'名'=>'人脸','邮件'=>'example2@example.com')
    );        如果($用户){
            $这个 - >响应($用户,200); // 200作为HTTP响应code
        }其他{
            $这个 - >响应(阵列('错误'=>'Couldn \\'吨发现任何用户),404!);
        }
    }
}

和试图通过类似的控制台来获取()集合的模式时:

 人民=新的人物();
peoples.fetch();
peoples.models;

它得到JSON响应,但仍表示有没有子对象(见图片)

http://i.stack.imgur.com/e5vZv.png

任何想法是怎么回事了?完全难住了!


解决方案

吃茶

这是正常的, people.models 是空的直接取指()调用之后,你需要等待Ajax请求的结束。

事实上,取()同步并骨干JS机制的文档说:


  

collection.fetch([选项])


  
  

[...]的选项散列接受会传递成功和错误回调(收集,响应)作为参数。<​​/ P>

来源: http://documentcloud.github.com/backbone/#Collection-fetch

解决方案

您需要使用:

 人民=新的人物();
peoples.fetch({
    成功:函数(收集,响应){
        //收集填充
        的console.log('型号:',peoples.models);
    }
});

I'm attempting to develop a site using CodeIgniter and Backbone.js, but I'm running into an issue when attempting to set Models to a Collection I have called fetch() on. I am using the REST API by Phil Sturgeon, and am receiving a JSON response when using fetch() on the Collection, but no children Models are added to it.

Here's the javascript I'm using:

$(document).ready(function() {
    window.Person = Backbone.Model.extend({});
    window.People = Backbone.Collection.extend({
        model: Person,
        url: "/api/content/users/format/json"
    });
});

And my CI Controller:

require APPPATH.'/libraries/REST_Controller.php';

class Content extends REST_Controller   {    
    function users_get()    {       
        $users = array(
            array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
            array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com')
    );

        if($users) {
            $this->response($users, 200); // 200 being the HTTP response code
        } else {
            $this->response(array('error' => 'Couldn\'t find any users!'), 404);
        }
    }
}

And when attempting to fetch() the Models for the Collection via the console like:

peoples = new People();
peoples.fetch();
peoples.models;

It gets the JSON response, but still says 'There are no child objects' (see image):

http://i.stack.imgur.com/e5vZv.png

Any idea what is going wrong? Totally stumped!

解决方案

Explications

It's normal that people.models is empty directly after the fetch() call, you need to wait the end of the ajax request.

Indeed, fetch() is asynchronous and the Backbone JS documention says :

collection.fetch([options])

[...] The options hash takes success and error callbacks which will be passed (collection, response) as arguments.

Source: http://documentcloud.github.com/backbone/#Collection-fetch

Solution

You need to use :

peoples = new People();
peoples.fetch({
    success: function (collection, response) {
        // The collection is filled
        console.log('Models : ', peoples.models);
    }
});

这篇关于Backbone.js的集合不应用模式(使用code点火器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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