Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用backbone.js从php获取返回值_Php_Codeigniter_Backbone.js - Fatal编程技术网

使用backbone.js从php获取返回值

使用backbone.js从php获取返回值,php,codeigniter,backbone.js,Php,Codeigniter,Backbone.js,您好,我有下面的codeIgniter控制器php代码,它将主干模型保存到服务器 public function generate() { //Converts JSON object to array $data = decode(TRUE); $this->load->library('db_models',$config=array('localhost', 'user', '343boys')); $data =

您好,我有下面的codeIgniter控制器php代码,它将主干模型保存到服务器

public function generate()
    {
       //Converts JSON object to array
       $data = decode(TRUE);
       $this->load->library('db_models',$config=array('localhost', 'user', '343boys'));
       $data = $this->db_models->getFields($data['database'],$data['table']);
       //Converts $data to JSON object
       $data = encode($data);
       //Problem is here how to i assign this data back to my model.
       return $data;

    }
我创建了一个JSON助手解码和编码,分别将JSON转换为数组和数组转换为JSON。这没有问题,因为我已经用firebug测试了它们。 代码在保存时工作正常,但在尝试检索return$数据时遇到了一个障碍;函数完成保存后

我的模特就是这样

var Table = Backbone.Model.extend({
     defaults:{
       'table':'mine',
       'database':'db'
},
        urlRoot : '/campusfeed/index.php/welcome/generate'

    });
model.save();

那么,我如何通过函数保存一个值,并在codeigniter中检索函数返回的不必要的内容。

有几个选项

model = new Table({
  table: 'yours'
  ,database: 'database'
});

model.save(null, {success: success});

传递给model.save的第一个对象是要更改的属性的散列。第二个参数是选项哈希。成功回调接收三个参数。更新的模型、来自服务器的响应和xhr对象。成功函数可以这样定义:

function success(model, response, xhr) {
  console.log('model saved', model, response, xhr);
}
model = new Table();
model.save(
  {
    table: 'yours'
    ,database: 'database'
  }
  ,{success: success}
);
function success(model, response, xhr) {
  console.log('model saved', model, response, xhr);
}