javascript - Backbone js - Clean up data using regex on collection(models) before passing to view -


i'm using backbone populate dropdown list. changes api have resulted in data (option labels) coming unwanted data e.g "option foo - 00011222" instead of "option foo"

i'm planning use regex can show first characters before '-'

what best approach sanitizing data on collection before rendering view.

should done on view or on collection

if additional data can safely discarded, can use model.parse modify data receive:

parse model.parse(response, options)
parse called whenever model's data returned server, in fetch, , save. function passed raw response object, , should return attributes hash set on model.

for example, if want replace label field:

var data = [     {label: 'option foo - 00011222'},     {label: 'option foo2 - 00011222'} ];  var m = backbone.model.extend({     parse: function(data) {         data.label = data.label.replace(m.regexp, '');         return data;     } }, {     regexp: /\s*-.*$/ });  var c = backbone.collection.extend({     model: m });  var c = new c(data, {parse: true}); console.log(c.pluck('label')); 

and demo http://jsfiddle.net/nikoshr/xpjdr/


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -