0

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?

Tarken
  • 2,112
  • 2
  • 23
  • 42

2 Answers2

0

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/

nikoshr
  • 32,926
  • 33
  • 91
  • 105
0

alternatively you can wrap the dynamic getter of your model with a getter wrapper: http://code.google.com/p/activejdbc/wiki/SettersAndGetters#No_%22standard%22_setters/getters???

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Can you provide an example? I looked at that page before, didn't help me, sry. As far as I understand it the problem is the attribute name not the getter. – Tarken Oct 02 '12 at 17:29
  • instead of doing this: user.get("address"); you add a getter: class User{ public String getAddress(){ return getString("address"); } } – ipolevoy Oct 09 '12 at 18:12