2012-07-18 107 views
0

我有一個屬性的視圖,我想更新它的值,當我提出一個獲取請求。骨幹查看屬性沒有設置

define(['underscore','backbone','models/taskCollection'], 
      function(_,Backbone,TaskCollection) { 
      var UserTasksView = Backbone.View.extend({ 
       el:"#user-task-list", 
       cur_task: undefined, 
       initialize: function() { 
        this.collection = new TaskCollection; 
        this.model = this.collection._model; 

        _.bindAll(this,'render'); 
        this.collection.bind('reset',this.render); 
       }, 
view_task: function(event) { 

       var el = $(event.currentTarget); 
       var task_id = el.attr('data-taskid'); 
       var row = el.parents('td').parents('tr.task-row'); 

       row.addClass("active"); 

       el.hide(); 
       el.next('a').show(); 

       var task = this.collection.fetch({ 
        data: {id:task_id}, 
        silent:true, 
        success:this._task_fetch_success 
       }); 

       this._show_task_detail(); 

       event.preventDefault(); 
      }, 

      _task_fetch_success: function(response,status,xhr) { 
       this.cur_task = JSON.stringify(status); 
       return status; 
      }, 

      /** 
      * Displays the details of a task 
      **/ 
      _show_task_detail: function() { 
       var main = $('.app-content'); 
       var detail_view = $('.app-extra'); 
       var task_detail_view = $("#task-detail-view"); 

       //Reduce task list view width 
       main.animate({ 
        "width":"50%" 
       },2000); 
       //Display app extra bar 
       detail_view.show(); 
       //show task detail view 
       detail_view.children('active-page').hide().removeClass('active-page').addClass('inactive-page'); 
       task_detail_view.show().removeClass('inactive-page').addClass('active-page'); 
       console.log(this.cur_task); 
       var template = ich.task_detail(this.cur_task) 
       $('div.task-details').html(template); 
      } 

由獲取Ajax請求觸發成功,成功回調執行,但是當我嘗試登錄「cur_task」屬性,它顯示爲不確定的; 我在做什麼錯

回答

1

你有這個權利在這裏開始了幾個問題:

var task = this.collection.fetch({ 
    data: {id:task_id}, 
    silent:true, 
    success:this._task_fetch_success 
}); 

這裏:

_task_fetch_success: function(response,status,xhr) { 
    this.cur_task = JSON.stringify(status); 
    return status; 
} 

首先,在success回調不是一個jQuery成功回調並沒有收到通常的jQuery參數;從:

options散列接受successerror回調將被傳遞(collection, response)作爲參數。

所以你_task_fetch_success功能被稱爲f(collection, response)不是f(response, status, xhr)因爲你期待;這就是爲什麼您必須將status參數視爲JSON:status實際上是response

您的下一個問題是,this是不是你認爲它在你的_task_fetch_success功能。骨幹fetch just calls success as a plain old function

var success = options.success; 
    options.success = function(resp, status, xhr) { 
    collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options); 
    if (success) success(collection, resp, options); // <--------------- Right here 
    collection.trigger('sync', collection, resp, options); 
    }; 

這意味着,thiswindow,而不是你的看法。解決此問題最簡單的方法是將_task_fetch_success添加到您的_.bindAll列表中initialize

initialize: function() { 
    //... 
    _.bindAll(this, 'render', '_task_fetch_success'); 
    //... 
}