Skip to content

Instantly share code, notes, and snippets.

@lyuehh
Created October 18, 2012 03:09
Show Gist options
  • Save lyuehh/3909659 to your computer and use it in GitHub Desktop.
Save lyuehh/3909659 to your computer and use it in GitHub Desktop.
extend a view in Backbone
// demo: http://jsfiddle.net/ShUW2/2/
var P = Backbone.View.extend({
events: {
'click p': 'p',
'change #a': 'change',
'click #s': 'click'
},
p: function (e) {
alert('clicked the p');
},
change: function (e) {
var $el = $(e.target);
console.log($el.val());
},
click: function () {
alert('click in p..');
}
});
var p1 = new P({el: '#parent'});
var C = P.extend({
initialize: function () {
P.prototype.initialize.apply(this);
},
p: function (e) {
alert('clicked the p in child');
}
});
var c1 = new C({el: '#child'});
@lyuehh
Copy link
Author

lyuehh commented Oct 19, 2012

extends events also:

var P = Backbone.View.extend({
    el: '#parent',
    events: {
        'click p': 'p',
        'change #a': 'change',
        'click #s': 'click'
    },
    p: function (e) {
        alert('clicked the p');
    },
    change: function (e) {
        console.log(e.target);
    },
    click: function () {
        alert('click in p..');
    }
});
var p1 = new P();
var C = P.extend({
    events: _.extend({},P.prototype.events,{
        'click #ccc': 'c'
    }),
    initialize: function () {
        P.prototype.initialize.apply(this);
    },
    el: '#child',
    p: function (e) {
        alert('clicked the p in child');
    },
    c: function () {
        alert('click c var extend ...');
    }
});
var c1 = new C();

@lyuehh
Copy link
Author

lyuehh commented Oct 19, 2012

demo: http://jsfiddle.net/ShUW2/3/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment