Search code examples
javahtmlbackbone.jsactivejdbc

ActiveJDBC + Backbone.js Generated attribute name not usable


I am using ActiveJDBC to access a third-party db. There is a column named "users/year". I am not happy with that but cannot change it. This results is an attribute "users/year" in the json thats the basis for my backbone.js model. Now if I want to access that attribute in the following template:

<li><%= name+ ' ' + users/year %></li>

Of course it doesn't work. I tried escaping but that doesn't work. Any suggestions?

Since I am new to activeJDBC and backbone.js is there a way off mapping the attribute to a acceptable variable name? Or other possible solutions for that? Should I switch from activeJDBC to something else?


Solution

  • You could add a parse method to your collection or model to remap your attribute to a usable name:

    var  M = Backbone.Model.extend({
        parse: function(resp) {
            if (resp['users/year']) {
                resp['users_year'] = resp['users/year'];
                delete resp['users/year'];
            }
    
            return resp;
        }
    });
    

    and change your template accordingly

    <li><%= name+ ' ' + users_year %></li>
    

    A Fiddle http://jsfiddle.net/nikoshr/rnKSD/