2011-08-03 55 views
0

我正在使用Titanium使用Web技術構建桌面應用程序。我決定使用Backbone.js作爲我的mvc。問題是應用程序不在服務器上運行。這是我的骨幹模型和收集:使用Backbone.js在離線應用程序中

var students = new Students 
students.fetch() 

我得到這個錯誤

 window.Student = Backbone.Model.extend({ 
     initialize: function(){ 
      this.bind("save", this.value_change); 
     }, 
     value_change: function(){ 
      alert("Student model saved for : " + this.attributes.first_name); 
     }, 

     urlRoot : http://localhost:8080/student/, 
    }); 

    window.Students = Backbone.Collection.extend({ 
     model: Student, 
     url: 'http://localhost:8080/students/', 
    }); 

,並嘗試從服務器獲取的值:

message: "'undefined' is not an object (evaluating '$.ajax')" 

我假定這必須做與URL部分。它無法從服務器獲取值。有任何想法嗎?

回答

0

感謝您的答案。加載jquery後,主幹問題奠定了。我首先加載jquery,它運行良好。感謝來自#documentcloud的irc的parshap。

0

collection.fetch()將在集合上觸發reset事件,而不是觸發save事件。保存是一種Model方法,用於在您的服務器上保存實例模型時執行POST請求到您的服務器。

你應該試試這個:

window.Student = Backbone.Model.extend({ 
}); 


window.Students = Backbone.Collection.extend({ 
    model: Student, 
    url: 'http://localhost:8080/students/', 

    initialize: function(){ 
     this.bind("reset", this.value_change); 
    }, 
    value_change: function(){ 
     alert("Students fetched "); 
    }, 
}); 

var students = new Students(); 
students.fetch(); 

我不知道你是什麼意思時,你說

The problem is the application runs not on a server

但是如果你的JavaScript並沒有在同一個域中的服務器上運行,您可能會遇到一些跨域javascript問題......這裏是一個使用Ruby on Rails的示例的帖子:http://www.tsheffler.com/blog/?p=428

2

問題是服務器上的骨幹保存模型。它通過發送一個jax請求到你的服務器來完成。你想要做的是覆蓋持久性機制

使用backbone.localStorage保存在localStorage的狀態而不是數據庫