Last active
December 25, 2015 18:19
-
-
Save GianlucaGuarini/7020116 to your computer and use it in GitHub Desktop.
Backbone + Rivets.js configuration and helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rivets.adapters[':'] = { | |
subscribe: function(obj, keypath, callback) { | |
if (obj instanceof Backbone.Collection) { | |
obj.on('add remove reset', callback); | |
} | |
obj.on('change:' + keypath, callback); | |
}, | |
unsubscribe: function(obj, keypath, callback) { | |
if (obj instanceof Backbone.Collection) { | |
obj.off('add remove reset', callback); | |
} | |
obj.off('change:' + keypath, callback); | |
}, | |
read: function(obj, keypath) { | |
return obj instanceof Backbone.Collection ? obj.models : obj.get(keypath); | |
}, | |
publish: function(obj, keypath, value) { | |
obj.set(keypath, value); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rivets.configure({ | |
templateDelimiters: ['{{', '}}'] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rivets.binders['toggle-class-*'] = function(el, value) { | |
$(el).toggleClass(this.args[0], !!(value)); | |
}; | |
rivets.binders['css-*'] = function(el, value, format) { | |
$(el).css(this.args[0],value); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rivets.formatters['ajaxhref'] = function (val) { | |
return '#!/' + val; | |
}; | |
rivets.formatters['length'] = function (val) { | |
return val.length; | |
}; | |
rivets.formatters['vocabulary'] = function(value, path) { | |
var vocabulary = App.vocabulary, | |
s = path, | |
a; | |
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties | |
s = s.replace(/^\./, ''); // strip a leading dot | |
a = s.split('.'); | |
while (a.length) { | |
var n = a.shift(); | |
if (n in vocabulary) { | |
vocabulary = vocabulary[n]; | |
} else { | |
return; | |
} | |
} | |
return vocabulary[value]; | |
}; | |
rivets.formatters.compare = function (lvalue, rvalue, operator) { | |
if (arguments.length < 3) | |
throw new Error("rivets formatter 'compare' needs 2 parameters"); | |
operator = operator || "=="; | |
var operators = { | |
'==': function (l, r) { | |
return l == r; | |
}, | |
'===': function (l, r) { | |
return l === r; | |
}, | |
'!=': function (l, r) { | |
return l != r; | |
}, | |
'<': function (l, r) { | |
return l < r; | |
}, | |
'>': function (l, r) { | |
return l > r; | |
}, | |
'<=': function (l, r) { | |
return l <= r; | |
}, | |
'>=': function (l, r) { | |
return l >= r; | |
}, | |
'typeof': function (l, r) { | |
return typeof l == r; | |
} | |
}; | |
if (!operators[operator]) | |
throw new Error("rivets formatter 'compare' doesn't know the operator " + operator); | |
var result = false; | |
if (typeof rvalue === "string") { | |
var valuesToCompareArray = rvalue.split(","); | |
_.each(valuesToCompareArray, function (value) { | |
if (operators[operator](lvalue, value)) { | |
result = true; | |
} else { | |
return; | |
} | |
}); | |
} else { | |
result = operators[operator](lvalue, rvalue); | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment