【问题标题】:How to get Spine.js Ajax persistence working如何让 Spine.js Ajax 持久性工作
【发布时间】:2012-11-14 16:28:20
【问题描述】:

为新应用学习 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>

【问题讨论】:

    标签: javascript ajax persistence spine.js


    【解决方案1】:

    是的,你用一个字符串来扩展它——这永远不会起作用。

    变化:

    Person.extend("Spine.Model.Ajax");

    收件人:

    Person.extend(Spine.Model.Ajax);

    【讨论】:

    • 非常感谢!!一切都非常出色。惊呆了,看了那么多遍都看不出来。我直接得到了作者的回复。 OReilly 的书写得很清楚。
    猜你喜欢
    • 2018-05-19
    • 1970-01-01
    • 2014-05-27
    • 2010-10-30
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多