学习新应用程序的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>发布于 2012-02-14 06:03:14
是的,你用一个字符串来扩展它--这是行不通的。
更改:
Person.extend("Spine.Model.Ajax");
至:
Person.extend(Spine.Model.Ajax);
https://stackoverflow.com/questions/9249400
复制相似问题