Santos
Santos

Reputation: 434

Migrate Mootools to prime or primish / Best way to use a JavaScript class

I want to migrate a project from mootools to prime or primish.

I need inherits classes, get options and get methods from the parent or grandparent, all things Mootools classes allowed me to do.

I think the best solution for me is to use primish and wrapup to send the classes.

At the moment my idea is this:

server:

var wrup = require("wrapup")();

wrup.require('prime', __dirname + '/primish/prime.js')
    .require('options', __dirname + '/primish/options.js')
    .require('emitter', __dirname + '/primish/emitter.js')
    .require('Element', __dirname + '/public/UI/Element.js')
    .require('otherClass', __dirname + '/public/UI/otherClass.js')
    .up(function(err, js){
        fs.writeFileSync(__dirname + '/public/main.js', js);
    });

app.get('/', function(req, res){

    res.sendfile('index.html');

});

index.html:

<script type="text/javascript" src="public/js/main.js"></script>

var el = new Element({
    options:{
        tag: 'div'
    }
});

Element.js:

var Element = prime({
    options: {
        tag: 'span'
    },
    some: function(){
        return;
    }
});

But I'm doing something wrong, I think that is the way I declare the class, but I'm not sure how to do.

Now how can I put this to work?

For who need all the functionalities of Mootools classes, what is the best alternative?

Some things were removed from prime, for reasons of performance.

Then my last question is for who wants start a new project using classes on the client side and the server side which is the best solution, taking account of performance.

I think the best solution would be to use pure JS like this, "method 2" and change the prototype to copy objects property, if we need.

Sorry so many questions, I'm trying to figure out what the best solution to implement classes in JS.

What are your opinions?

Sorry i forget to implement: options, and the constructor, but this is not the problem, if I send the classes in the index like this:

<script type="text/javascript" src="public/UI/Element.js"></script>
<script type="text/javascript" src="public/UI/otherClass.js"></script>

works well, but I using wrapup to send the classes and is not working.

Upvotes: 2

Views: 272

Answers (1)

n3on
n3on

Reputation: 2090

Prime will probably never cover all the things that MooTools does. It is a completely different approach, you just load the modules you need from npm (or from any other source) and use what would work best for your purpose. Primish is a prime fork from Dimitar Christoff which implements some of the MooTools class logic (and some other stuff as well, take a look at the readme here: https://github.com/DimitarChristoff/primish/ ).

Why do you want to switch from MooTools to Prime / Primish if MooTools covers all your use cases?

Concerning your code problems: You need to set a constructor (initialize in MooTools):

var Element = prime({

    implement: [options],

    constructor: function(options) {
        this.setOptions(options);
    },

    options: {
        tag: 'span'
    },

    some: function(){
        return;
    }
});

Then you can instantiate your Class like this:

var el = new Element({
    tag: 'div'
});

Upvotes: 2

Related Questions