2013-02-04 51 views
0

我正在嘗試設置一個我在jsfiddle上製作的應用程序的演示,以便獲得有關問題的幫助。它將需要進行ajax調用。 jsFiddle有一個回聲api(exampel在這裏撥弄http://jsfiddle.net/zalun/yVbYQ/)來模擬ajax調用,但我無法弄清楚如何將它集成到骨幹模型或集合中,比如我在下面。這是一個骨幹模型的小提琴... http://jsfiddle.net/mjmitche/RRXnK/117/如果這樣的協助。在jsfiddle上用Backbone嘲弄ajax請求

var Game = Backbone.Model.extend({ 

    initialize: function() { 

    }, 
    getStuff: function() { 
     var _this = this; 

     $.ajax({ 
      url: "http://search.twitter.com/search.json?q=from:realDonaldTrump", 
      type: "GET", 
      success: function (response) { 
       alert(response.results[0]); 
       alert("success"); 
       _this.trigger("gameStartedEvent", response); 
      }, 
      error: function (r) { 
       alert("error"); 
       alert(r); 

      } 
     }); 

    } 

}); 

var game = new Game(); 
game.getStuff(); 

所述的jsfiddle模擬AJAX調用

new Request.JSONP({ 
    url: 'http://jsfiddle.net/echo/jsonp/', 
    data: { 
     tweet1: 'some text', 
     tweet2: 'another text' 
     tweet3: 'blah blah' 
    }, 
    onSuccess: function(response) { 
     show_response(response, $('post')); 
    } 
}).send(); 

show_response = function(obj, result) { 
    $H(obj).each(function(v, k) { 
     new Element('li', { 
      text: k + ': ' + v 
     }).inject(result); 
    }); 
    result.highlight(); 
}; 

回答

0

的回波的jsfiddle API不支持實現骨幹CRUD需要HTTP操作的範圍的一個例子。使用POST /echo您可以模擬創建新模型,但就是這樣。

請改用sinon.js fakeServerautoRespond選項來模擬客戶端上的服務。這將需要更多的工作,但會提供更「真實」的模擬。

FWIW,對於正常的CRUD使用情況,我會推薦Backbone.localStorage。這是Backbone.sync的替代替代品,因此只需將其作爲資源包含在內,就可以將您的收藏的url屬性更改爲localStorage,而且您已經開始營業了。但是,這對你不起作用,因爲你手動提交AJAX請求,所以你需要降低。