Skip to content

Instantly share code, notes, and snippets.

grunt.initConfig({
less: {
src: ['styles.less'],
dest: 'styles.css'
},
postcss: {
options: {
processors: [
require('autoprefixer')(),
]
css:
lessc style.less | autoprefixer-cli > style.css
var Router = Backbone.Router.extend({
before: function() {
console.log('before');
},
after: function() {
console.log('after');
},
routes: {
// save a reference to route() method
var $route = Backbone.Router.prototype.route;
// then override it
Backbone.Router.prototype.before = function() {}
Backbone.Router.prototype.after = function() {}
Backbone.Router.prototype.route = function(route, name, callback) {
if (_.isFunction(name)) {
callback = name;
name = '';
var coll = new Backbone.Collection([model1, model2]);
console.log(coll.search('Jordan').first().get('last_name')); // Aslam
console.log(coll.search('John').first().get('last_name')); // Doe
console.log(coll.search(/Jo/).pluck('last_name')); // ['Aslam', 'Doe']
console.log(coll.search(/Jordan|John/).pluck('last_name')): // ['Aslam', 'Doe']
Backbone.Collection.prototype.search = function(test) {
return this.filter(function(model) {
return model.match(test);
});
}
var model1 = new Backbone.Model({ first_name: 'Jordan', last_name: 'Aslam' });
var model2 = new Backbone.Model({ first_name: 'John', last_name: 'Doe' });
console.log(model1.match(‘Jordan’)); // true
console.log(model2.match(‘Doe’)); // true
console.log(model1.match(/Jo/)); // true
console.log(model2.match(/Jo/)); // true
Backbone.Model.prototype.match = function(test) {
return _.any(this.attributes, function(attr) {
return _.isRegExp(test) ? test.test(attr) : attr == test;
});
}
Backbone.View.extend({
initialize: function() {
// obviously .foo element do not yet exists because view is not rendered
// this line will throw an error
this.$(.foo’).text(‘foo-bar-baz’);
}
});
// save a reference to $() method
var view$ = Backbone.View.prototype.$;
// then override it
Backbone.View.prototype.$ = function(selector) {
var element = view$.apply(this, arguments);
if (!element.length === 0) {
console.error("[Backbone.View] Warning: selector '" + selector + "' do not match any element");
}