Santos
Santos

Reputation: 434

Classes, Options in Mootools/Prime

I will migrate a project from Mootools to Prime, but now I have a problem with the "setOptions", the function does not work as in Mootools where the options object will merge through supers up the proto chain and contain all the keys.

var A = prime({
    options: {
        name: 'image',
        tag: 'span',
        src: '/'
    },
    constructor: function(options){
        this.setOptions(options);
    },
    getOptions: function(){
        console.log(this.options);
    }
});

var B = prime({
    inherits: A,
    options: {
        name: 'B'
    },
    some: function(){
        return;
    }
});

mixin(A, Options);
mixin(B, Options);

var b = new B({
    tag: 'div'
});

b.getOptions(); //the result should be: {name: "B", tag: "div", src: "/"} but is {name: "B", tag: "div"}

Thanks for any idea.

Upvotes: 2

Views: 142

Answers (1)

Dimitar Christoff
Dimitar Christoff

Reputation: 26165

As answered on the mailing list - I even made you a repo - this works out of the box in primish.

https://bitbucket.org/DimitarChristoff/primish-example/src

slightly different syntax (sugar, it's been done to make mootools users happy) but it does also merge options like mootools did:

var A = prime({
    options: {
        name: 'image',
        tag: 'span',
        src: '/'
    },
    implement: [options],
    constructor: function(options){
        //console.debug(this.options);
        this.setOptions(options);
    },
    getOptions: function(){
        console.log(this.options);
    }
});

var B = prime({
    extend: A,
    options: {
        name: 'B'
    }
});

var C = prime({
    extend: B,
    options: {
        name: 'C'
    }
});

var c = new C({
    tag: 'div'
});

c.getOptions(); // as expected, all 3 options merged from right to left

Notice distinct lack of the extra mixin() nonsense and the familiar extend term rather than inherits

In prime you'd have to do something like:

var B = prime({
    extend: A,
    options: Object.merge(A.prototype.options, {
        name: 'B'
    })
});

where Object.merge is some util - either a prime util or lodash or whatever. in primish that would be prime.merge()

Upvotes: 3

Related Questions