1

我正在尝试使用backbone.js来包装Jenkins REST API。

要获取工作详细信息,可以对这样的 URL 执行 GET:

https://jenkins.example.com/jenkins/job/test-job/api/json/?jsonp=jQuery18207471012645401061_1351262357807&_=1351262357850

我非常简单的主干应用程序如下所示:

$(function () {
   var Job = Backbone.Model.extend({
      defaults:{
         displayName:'not set'
      }
   });

   var AppView = Backbone.View.extend({
      el:$("#hudApp"),

      initialize:function () {
         var job = new Job;
         job.url = 'https://jenkins.example.com/jenkins/job/test-job/api/json/?jsonp=?';
         job.fetch({dataType:"jsonp"});

         alert(job.get('displayName'));
      });
   });

   var app = new AppView;
});

我可以看到 HTTP 请求到达我的 Jenkins 服务器,并收到如下响应:

jQuery18207471012645401061_1351262357807(
{
description: "build a nice test job",
displayName: "test-job",
}
)

但是,我的模型没有得到更新(alert()总是显示“未设置”)。

谁能发现我做错了什么?

4

1 回答 1

3

Complete operator error. Apparently, the "A" in AJAX stands for "asynchronous"...

I just needed to change my fetching code to:

        job.fetch({
           dataType:"jsonp",
           success: function(model, response) {
              alert(model.get('displayName'));
           }
        });

Sorry about the stupid question. Hopefully this is at least useful to Google, as I had lots of trouble finding examples of how to use Backbone with JSONP.

于 2012-10-26T15:21:59.933 回答