骨干事件触发点击或preSS进入 [英] Backbone event firing on click OR press enter

查看:143
本文介绍了骨干事件触发点击或preSS进入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的骨干,我想给我的按钮的方式来触发,当我preSS <大骨节病>输入以及点击。目前showPrompt只执行一个点击。什么是有它在执行pressing干净DRYest方式<大骨节病>输入还有,preferably只为输入字段。

I am new to backbone and I am looking for a way for my button to be triggered when I press Enter as well as clicking. Currently showPrompt only executes on a click. What is the cleanest DRYest way to have it execute on pressing Enter as well, preferably only for that input field.

(function () {

  var Friend = Backbone.Model.extend({
    name: null
  });

  var Friends = Backbone.Collection.extend({
    initialize: function (models, options) {
      this.bind("add", options.view.addFriendLi);
    }
  });

  var AppView = Backbone.View.extend({
    el: $("body"),
    initialize: function() {
      this.friends = new Friends(null, {view: this});
    },
    events: {
      "click #add-friend":  "showPrompt",
    },
    showPrompt: function () {
      var friend_name = $("#friend-name").val()
      var friend_model = new Friend({ name:friend_name });
      this.friends.add( friend_model );
    },
    addFriendLi: function (model) {
      $("#friends-list").append("<li>" + model.get('name') + "</li>");
    }
  });

  var appView = new AppView; 

}());

另外我在哪里可以阅读更多关于这种类型的事件绑定?不要骨干事件从JS或jQuery的事件,他们是如何定义的有什么不同?

Also where can I read more about this kind of event binding? Do backbone events differ from JS or jQuery events in how they're defined?

推荐答案

假设你正在使用jQuery的DOM操作。创建一个微小的插件触发该<大骨节病>输入中输入事件。把它放在你的plugins.js或任何安装脚本文件,您有:

Assuming that you are using jQuery for DOM manipulation. Create a "tiny" plugin that fires the Enter event in the inputs. Put it at your plugins.js or whatever setup scripts file that you have:

  $('input').keyup(function(e){
   if(e.keyCode == 13){
      $(this).trigger('enter');
   }
 });

具有输入插件,Backbone.js的应该是简单的东西,因为这:

Having the enter plugin, Backbone.js should be something as simple as this:

events: {
   "click #add-friend":  "showPrompt",
   "enter #friend-name" : "showPrompt"
}

这篇关于骨干事件触发点击或preSS进入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆