0

例如,我有这个模型作为用户:

var Users = Spine.Model.sub();
Users.configure('Users', 'name', 'gender', 'age');
Users.extend(Spine.Model.Ajax);
Users.extend({url:"/users"});

假设我们已经在数据库中保存了一些数据。如果运行

var users = Users.fetch();

Ajax 会向 /users 发送一个 GET 请求,所有结果都会返回。

但是如果我想获取所有女性或男性用户,或年龄在 25 岁以上,或按指定顺序排名前 10 位的用户,如何传递这些变量?我在文档中找不到规范。fetch 方法可以通过回调函数参数在 fetch 完成时撤销,显然不是我想要的。

4

1 回答 1

1

我自己找到了解决方案。实际上该文档告诉了如何对结果进行分页。

var Photo = Spine.Model.sub();
Photo.configure('Photo', 'index');
Photo.extend(Spine.Model.Ajax);

Photo.extend({
  fetch: function(params){
    if ( !params && Photo.last() ) 
      params = {data: {index: this.last().id}}
    this.constructor.__super__.fetch.call(this, params);
  }
});

但我发现代码无法运行,首先

this.constructor.__super__.fetch.call(this, params);

应该

this.__super__.constructor.fetch.call(this, params);

其次,如果运行 Photo.fetch({data:{id:1}}),它将像这样发送一个 GET 请求

GET /photos?[object%20Object]

纠正它

Photo.fetch({data: $.param({id:1})});

HTTP 请求

GET /photos?id=1
于 2013-03-08T11:48:04.820 回答