Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
Javascript 主干:在变量中输入JSON,而不是通过URL获取_Javascript_Php_Jquery_Json_Backbone.js - Fatal编程技术网

Javascript 主干:在变量中输入JSON,而不是通过URL获取

Javascript 主干:在变量中输入JSON,而不是通过URL获取,javascript,php,jquery,json,backbone.js,Javascript,Php,Jquery,Json,Backbone.js,我们正在尝试修改一个现有脚本,该脚本使用backbone.js从URL获取JSON,并在屏幕上以定义的方式呈现它 早些时候,脚本指向一个外部PHP文件以从中获取JSON url: function () { var ajaxValue = document.getElementById('ajax').value; if(ajaxValue==0){ return this.options.apiBase + '/liveEvents.json

我们正在尝试修改一个现有脚本,该脚本使用backbone.js从URL获取JSON,并在屏幕上以定义的方式呈现它

早些时候,脚本指向一个外部PHP文件以从中获取JSON

url: function () {          
    var ajaxValue = document.getElementById('ajax').value;
    if(ajaxValue==0){
        return this.options.apiBase + '/liveEvents.json';           
    } else {
        var eventDate = document.getElementById('timestamp').value;
        return this.options.apiBase + '/ajax.php?eventDate='+eventDate;         
    }
},
但现在我们试图忽略PHP的要求,而纯粹使用Javascript获取JSON。为此,我们创建了一个JS函数fetch_data_set(),该函数返回正确的JSON

var ArrayMerge = array1.concat(array2,array3,array4);
return JSON.stringify(ArrayMerge);
所以我们的问题是,我们如何将这个JSON提供给主干网,而不是使用外部URL。因为如果我们这样做(这显然是错误的):


它抛出错误:错误:必须指定“url”属性或函数

主键是扩展
主干.sync
而不是url()方法,因此您可以使用这种方式获取任何类型模型中的模型,并且可以执行类似于以下链接的操作:


Backbone.Model包含一个
sync()
函数,可以从url加载JSON数据
sync()
使用
url()
函数确定从何处获取数据。(注意:
sync()
在引擎盖下由
save()
fetch()
destroy()
调用)

这里的技巧是,您应该停止重写
url()
,而是直接重新实现
sync()
,cf

以下是一个例子:

// specialized version to be used with a store.js - like object
sync: function(method, model, options) {
  console.log("sync_to_store begin('"+method+"',...) called with ", arguments);
  var when_deferred = when.defer();

  var id = this.url();

  if(method === "read") {
    if(typeof id === 'undefined')
      throw new Error("can't fetch without id !");
    var data = model.store_.get(id);
    // apply fetched data
    model.set(data);
    when_deferred.resolve( [model, undefined, options] );
  }
  else if(method === "create") {
    // use Backbone id as server id
    model.id = model.cid;
    model.store_.set(id, model.attributes);
    when_deferred.resolve( [model, undefined, options] );
  }
  else if(method === "update") {
    if(typeof id === 'undefined')
      throw new Error("can't update without id !");
    model.store_.set(id, model.attributes);
    when_deferred.resolve( [model, undefined, options] );
  }
  else if(method === "delete") {
    if(typeof id === 'undefined')
      throw new Error("can't delete without id !");
    model.store_.set(id, undefined);
    model.id = undefined;
    when_deferred.resolve( [model, undefined, options] );
  }
  else {
    // WAT ?
  }

  console.log("sync_to_store end - Current changes = ", model.changed_attributes());
  return when_deferred.promise;
}
  • 注1:API与香草主干略有不同,因为我返回 什么时候的承诺
  • 注2:
    url()
    仍然用作id
// specialized version to be used with a store.js - like object
sync: function(method, model, options) {
  console.log("sync_to_store begin('"+method+"',...) called with ", arguments);
  var when_deferred = when.defer();

  var id = this.url();

  if(method === "read") {
    if(typeof id === 'undefined')
      throw new Error("can't fetch without id !");
    var data = model.store_.get(id);
    // apply fetched data
    model.set(data);
    when_deferred.resolve( [model, undefined, options] );
  }
  else if(method === "create") {
    // use Backbone id as server id
    model.id = model.cid;
    model.store_.set(id, model.attributes);
    when_deferred.resolve( [model, undefined, options] );
  }
  else if(method === "update") {
    if(typeof id === 'undefined')
      throw new Error("can't update without id !");
    model.store_.set(id, model.attributes);
    when_deferred.resolve( [model, undefined, options] );
  }
  else if(method === "delete") {
    if(typeof id === 'undefined')
      throw new Error("can't delete without id !");
    model.store_.set(id, undefined);
    model.id = undefined;
    when_deferred.resolve( [model, undefined, options] );
  }
  else {
    // WAT ?
  }

  console.log("sync_to_store end - Current changes = ", model.changed_attributes());
  return when_deferred.promise;
}