[英]backbone.js model.fetch sends no data to php
I'm just starting to play around with backbone.js to learn more about it for a project where I use Javascript for client side code an PHP on the server side. 我刚刚开始使用ribs.js来了解一个项目的更多信息,在该项目中,我使用Javascript作为客户端代码,而在服务器端使用PHP。
To find out how communications between backbone and the server actually works (can't really find any good documentation about it. I just made a small example for fetch()
: 为了找出骨干网与服务器之间的通信实际上是如何工作的(实际上找不到关于它的任何好的文档。我只是为
fetch()
做了一个小例子:
var NavigationElement = Backbone.Model.extend({
initialize: function() {},
defaults: {
text: 'Home',
link: '/',
category: 'Main'
},
url: 'ajax.php'
});
var forum = new NavigationElement( { text: 'Forum',
link: '?q=Forum'});
var login = new NavigationElement( { text: 'Login',
link: '?q=Login',
category: 'User'});
forum.fetch();
Now, Backbone should send some GET
data to the server to identify the data it has to send. 现在,Backbone应该向服务器发送一些
GET
数据,以标识它必须发送的数据。 And indeed, $_SERVER[ 'REQUEST_METHOD' ]
says there is a GET-Request, but $_GET
remains empty. 实际上,
$_SERVER[ 'REQUEST_METHOD' ]
表示存在一个GET-Request,但是$_GET
仍然为空。
For save()
I had a similar problem and learned, that I could find the data in php://input
for all POST
and PUT
Requests. 对于
save()
我有一个类似的问题并了解到,可以在php://input
找到所有POST
和PUT
请求的数据。 Is there a similar stream for GET
Requests? GET
请求有类似的流吗?
And how can I tell Backbone to send a GET-Request like '.../ajax.php?model=NavigationElement&text=Login' ?
以及如何告诉Backbone发送GET请求,例如
'.../ajax.php?model=NavigationElement&text=Login' ?
By default, model.fetch
will use the first available in 默认情况下,
model.fetch
将使用第一个可用的
fetch
fetch
的选项中传递的url属性 model.url
model.url
设置了model.url
model.urlRoot
with /id
appended if the id
attribute exists id
属性,则附加带/id
model.urlRoot
model.collection.url
with /id
appended if the id
attribute exists id
属性,则在model.collection.url
后面附加/id
You can provide a custom url
method on NavigationElement
to set a different endpoint. 您可以在
NavigationElement
上提供自定义url
方法以设置其他端点。 For example: 例如:
var NavigationElement = Backbone.Model.extend({
initialize: function() {},
defaults: {
text: 'Home',
link: '/',
category: 'Main'
},
url: function() {
return 'ajax.php?model=NavigationElement&text='+encodeURIComponent(this.get('text'));
}
});
A demo : http://jsfiddle.net/nikoshr/ygdbxn1q/ 演示: http : //jsfiddle.net/nikoshr/ygdbxn1q/
or if you just want to pass additional parameters on read
operations, you can override fetch
and pass the data as an option: 或者,如果您只想在
read
操作中传递其他参数,则可以覆盖fetch
并将数据作为选项传递:
var NavigationElement = Backbone.Model.extend({
initialize: function() {},
defaults: {
text: 'Home',
link: '/',
category: 'Main'
},
url: 'ajax.php',
fetch: function(options) {
options = options || {};
options.data = _.extend({
model: 'NavigationElement'
}, this.pick('text'));
return Backbone.Model.prototype.fetch.call(this, options);
}
});
http://jsfiddle.net/nikoshr/ygdbxn1q/1/ http://jsfiddle.net/nikoshr/ygdbxn1q/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.