Search code examples
backbone.jsmodelinitialization

backbone model initialize function


I don't understand why backbone is adding the argument of the initialize function to the model.

var RippleId = Backbone.Model.extend({

    initialize: function(toresolve) {
        this.url= config.rippleaccount.id.urlModel+toresolve;       
    }

});

toresolve is a character chain (something like that "rkjnfezmeznfzln..." and the result when i create a model is that

0: "r"1: "h"2: "s"3: "b"4: "z"5: "U"6: "t"7: "o"8: "N"9: "Z"10: "C"11: "t"12: "2"13: "7"14: "Y"15: "L"16: "Q"17: "c"18: "k"19: "K"20: "V"21: "Q"22: "H"23: "n"24: "E"25: "g"26: "f"27: "Y"28: "y"29: "g"30: "J"31: "Q"32: "b"33: "5"account_data: Objectid: "keyfact4"ledger_current_index: 10414762validated: false

Basically my object is ok. But at the beginning each character of my chain has been added as an argument. It seems backbone is adding it considering the chain as an array.

I don't understand why backbone is doing that, am I doing something wrong ?

thanks a lot in advance


Solution

  • So, toresolve is a string? Backbone.Model expects the first parameter to be the attributes of the model, and second parameter to be the options. Both should be objects.

    I think what you want is:

    var options = {
        toresolve: 'rkjnfezmeznfzln'
    };
    new RippleId({}, options);
    

    and then in your initialize function:

    initialize: function (attr, options) {
        this.url = config.rippleaccount.id.urlModel + options.toresolve; 
    }