2013-07-15 30 views
1

所以,我是一个java开发人员,试图不在JavaScript中编写太多的java代码。我已经阅读了关于JavaScript的各种资源(例如http://chimera.labs.oreilly.com/books/1234000000262/ch04.html),但我仍然无法弄清楚这样做的一个整洁和好的方法。使用对象作为事件处理程序

因此,根本的问题是:

  • 点击“删除”按钮(会出现在页面上的许多删除按钮)
  • 开放的引导模式对话框,这是一个下划线模板
  • 绑定行动到按钮
  • 如果正常点击,使Ajax请求

所以,这是一个部分,到目前为止有效的解决方案。 我有问题从ajax成功回调调用函数。如果我尝试使用“this”,它的上下文是错误的。我可以将其存储在另一个变量中(比如'that'),但那肯定会很好。

而且我不太清楚这个代码看起来那么好,考虑到我在上面提到的书已经阅读

function DeleteHandler() { 
    var obj = { 
     init: function() { 
      _.bindAll(this, 'render', 'close', 'submit'); 

      this.$main = $('#main'); 
      // get underscore template 
      this._template = _.template($('#delete-template').html()); 
      // bind function to delete button 
      this.$main.on('click', '.delete', this.render); 


     }, 
     render: function(e) { 
      e.preventDefault(); 
      //render the template and bind button actions 
      this.$content = $(this._template({title: 'moe'})); 
      this.$content.on('click', '.ok', this.submit); 
      this.$content.modal('show'); 
      this.$endpoint = $(e.target).attr('endpoint'); 
     }, 
     close: function() { 
      this.$content.modal('hide'); 
     }, 
     submit: function() { 
      $.ajax({ 
       url: this.$endpoint, 
       type: 'DELETE', 
       success: function(data,textStatus){ 
        // call close function here! but how?!? 

       }, 

      }); 
     } 
    } 
    return obj; 
}; 

现在我可以使用这样的

<span class="delete" endpoint='http://....'>delete</span> 

<script type="text/javascript"> 

    $(function($) { 
     DeleteHandler().init(); 
    }); 
</script> 

我会很开心如果我可以像这样打电话给我的功能:

DeleteHandler.init(); 

这是可能的吗?我将在页面上多次使用这个函数,所以我不能只使用文字而不是函数。

编辑: 我找到某种解决办法,使Ajax回调发生: 您可以通过上下文对jQuery AJAX文字:

如果我用的是这样的:

$.ajax({ 
    url: this.$endpoint, 
    type: 'DELETE', 
    context: this, 
    success: function(data,textStatus){this.$update.html(data); this.close();}, 
} 

我实际上可以在成功回调中调用this.close()。可能不是一个很好的解决方案。但可能有人有更好的主意?

+0

这个问题似乎是脱离主题,因为它是关于代码重构,它应该继续http://codereview.stackexchange.com/。 – Claudiu

+0

这实际上并不涉及代码重构。我正在努力寻找一个解决特定问题的好方法。我已经提供了我目前为止的内容,以便更容易理解并指出具体的观点。如果没有我的示例代码,那个问题就可以工作,但需要更多的努力来回答。但可能codereview页面是这样的问题的好地方。 –

+0

由于您使用的是jQuery,因此一种方法是创建一个插件(从[here](http://learn.jquery.com/plugins/)中的链接开始)。还有其他一些方法可以完成你已经做的事情,比如将'init'代码移入构造函数,将其他方法移入'TheConstructor.prototype.methodName',然后使用'new TheConstructor(args)'实例化。你选择实施“伪类”的方法部分是风格问题。也许你可以提供更多的细节,为什么你现在的代码不“感觉正确”。 – bfavaretto

回答

1

您已经通过DeleteHandler函数将函数包装到一个对象中(函数在技术上是对象)。而不是使VAR obj的,你可以声明初始化函数您DeleteHandler函数中,并调用它...

function DeleteHandler() { 
    // Declare the Init function inside our DeleteHandler object 
    function Init() {do stuff...}; 

    // Declare your other functions and 'stuff' 
    function render() {...}; 

    // Now call the function. 
    Init(); 
}; 

至于创建和使用的对象,它会看起来更像这个:

<span class="delete" endpoint='http://....'>delete</span> 

<script type="text/javascript"> 

    $(function($) { 
     DeleteHandler(); 
    }); 
</script> 
+0

如果你喜欢我,并且来自像C++这样的面向对象的语言,我喜欢想像这样的格式,比如声明一个类。 DeleteHandler是一个类,它内部是变量和函数。稍有不同的是,不要声明构造函数,而是直接在“类”中进行构建。 – Lochemage

+0

我已经阅读过这本书,我提到,构建函数并不是一个好习惯,因为javascript实际上并没有构造函数。但是,是的,也许我确实越来越偏执;)就个人而言,我不喜欢在该方法的声明中显式调用构造函数。我已经更新了我正在尝试编写这段代码的另一个问题。也许你也有一个好主意。 –

相关问题