2013-07-17 1 views
4

Я использую Backbone и bootbox. Это мой код внутри вида:Передайте «это» обратному сообщению bootbox с Backbone

confirm : function(result) { 
     if (result === true) { 
      var that = this; 
      this.model.set({completed: '1'}); // Exception here 
      this.model.save(
        null, { 
       success: function (model, response) { 
        Backbone.history.navigate("index", true); 
       }, 
       error: function(model, response) { 
        that.model.set({completed: '0'}); 
        var responseObj = $.parseJSON(response.responseText); 
        bootbox.alert(responseObj.message); 
       } 
      }); 
     } 
    }, 

    completeProcess : function(event) { 
     event.preventDefault(); 
     this.model.set({completed: '1'}); 
     bootbox.confirm("Confirm?", this.confirm); 
    } 

Я получаю эту ошибку:

Uncaught TypeError: Cannot call method 'set' of undefined

Есть ли способ, чтобы передать ссылку с точки зрения?

+0

Вы посмотрели на [_.bind] (http://underscorejs.org/#bind)? – Jack

+0

@Jack Что такое значение '_.bind' над стандартным' Function.prototype.bind'? Обратная совместимость? Больше ничего не вижу. –

+0

@JustinMorgan Если стандартный (или, скорее, * native *) 'bind' доступен, то подчеркивание будет использоваться вместо этого. – Jack

ответ

3

Как является зависимость вы могли бы использовать его _.bind особенность:

_.bind(function, object, [*arguments]) 

Bind a function to an object, meaning that whenever the function is called, the value of this will be the object.
Optionally, pass arguments to the function to pre-fill them, also known as partial application.

В вашем случае это может выглядеть следующим образом:

completeProcess : function(event) { 
    event.preventDefault(); 
    this.model.set({completed: '1'}); 
    bootbox.confirm("Confirm?", _.bind(this.confirm, this)); 
} 
+0

вопрос: как насчет исходного параметра "result", переданного bootbox? Я имею в виду, это просто игнорируется? – dierre

+0

, поэтому я попробовал, но я все еще получаю результат от подтверждения, а не от этой ссылки. Мне нужны оба. – dierre

+0

извините! Получил это, понял. Большое спасибо! – dierre

0

В качестве альтернативы, вы можете сделать что-то как показано на рисунке:

var _this = this; 

bootbox.confirm({ 
    message: Message , 
    callback: function(result) { 
     console.log(this + " != " + _this); 
    }, 

 Смежные вопросы

  • Нет связанных вопросов^_^