为新应用程序学习 Spine.js。 Spine 框架很优雅。我不太能够让 Ajax 持久性工作。没有错误消息,只是没有事件。
我有一个 REST api http://localhost:8080/services/api/myservice/persons
通过浏览器和 jQuery.ajax(...)
返回 Person 对象的 JSON 列表在此最小测试中调用以下命令。
但是调用 Person.fetch() 不会产生 ajaxSuccess
也不ajaxFailure
,也不会触发 refresh
事件。服务器也不注册请求。我包含了“ajax.js”并扩展了 Spine.Model.Ajax。想必我缺少一个基本设置,将不胜感激您的提示。
<script type="text/javascript" src="/appLib/jquery-1.7.js"></script>
<script type="text/javascript" src="/appLib/spine/spine.js"></script>
<script type="text/javascript" src="/appLib/spine/ajax.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
Spine.Model.host = "http://localhost:8080/services/api/myservice";
var Person = Spine.Model.sub();
Person.configure("firstName", "lastName");
Person.extend("Spine.Model.Ajax");
// these callbacks are never called
Person.bind("refresh", function() {alert("refreshed "+Person.count());}); // never called
Person.bind("ajaxSuccess", function() {alert("ajax success");}); // never called
Person.bind("ajaxError", function(record, xhr, settings, error) {
alert("ajaxError"+error); // never called
});
Person.fetch(function() {alert("Fetch "+Person.count());}); // 0
Person.each(function() {alert("Found");}); // never called
// but this direct ajax call works fine
jQuery.getJSON(Spine.Model.host + "/persons", function(result) {
for (var i=0, l=result.length; i<l; i++) {
var person = Person.init(result[i]);
person.save();
}
alert(Person.count()); // shows correct result
Person.each(function(p) {alert(p.lastName);}); // shows correct results
});
});
</script>
最佳答案
是的,你用字符串扩展它 - 这是永远行不通的。
更改:
Person.extend("Spine.Model.Ajax");
致:
Person.extend(Spine.Model.Ajax);
关于javascript - 如何让 Spine.js Ajax 持久性工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249400/