Giter VIP home page Giter VIP logo

Comments (7)

petersirka avatar petersirka commented on August 23, 2024

Please test it. After the view() is cleaned a controller.

    if(post.userID) {

        // model.ID = post.userID;

        self.database('users').one(function(doc) {

            //does user with 'userID' exist
            return doc.ID === post.userID;

        }, function(doc) {

            model = doc;

            self.view('settings/user-edit', model);
        });

        // ========================================
        // HERE HERE HERE HERE
        // ========================================
        return;
    }

Code review:

function view_user_edit() {
    var self = this;
    var post = self.post;

    if (!post.userID) {
        self.view('settings/user-edit', { ID: '' });
        return;
    }

    self.database.one(expression('doc.id === post.userID', ['doc', 'post'], post), function(doc) {
        self.view('settings/user-edit', doc);
    });
}

from framework.

eMarek avatar eMarek commented on August 23, 2024

Oh jeah, return; was missing. Thank you.

About code review.. Could you please explain how does expression function work. Documentation is only indicative. Anyway, I like it, it is cleaner than my version. :)

from framework.

petersirka avatar petersirka commented on August 23, 2024

"expression" returns a function and the "return value" is executed string. This is "magic" 🎱. Now I think that is bad practice because function will be executed every time and this is slowly than classic callback function - really sorry. Every coin has two side, one side is less code and other side is slowly performance - you must choose (I'm for the performance).

"expression" use:

function arrayFindAll(arr, filter) {

    var length = arr.length;
    var selected = [];

    for (var i = 0; i < length; i++) {
        var value = arr[i];
        if (filter(value))
            selected.push(value);
    }

    return selected;
};

function arrayFindAllWithIndex(arr, filter) {

    var length = arr.length;
    var selected = [];

    for (var i = 0; i < length; i++) {
        var value = arr[i];
        // i === index
        if (filter(value, i))
            selected.push(value);
    }

    return selected;
};

console.log(arrayFindAll([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], expression('value > 5', ['value'])));
console.log(arrayFindAll([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], expression('value > myvalue && value < myvalue + 5', ['value', 'myvalue'], 3)));

// expression('value > 5', ['value'])
function(value) {
    return value > 5;
};

// expression('value > myvalue && value < myvalue + 5', ['value', 'myvalue'], 3)))
function(value, myvalue) {
    return value > myvalue && value < myvalue + 5;
};

// We must add index param
console.log(arrayFindAllWithIndex([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], expression('value > 5', ['value', 'index'])));
console.log(arrayFindAllWithIndex([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], expression('value > myvalue && value < myvalue + 5', ['value', 'index', 'myvalue'], 3)));

// expression('value > myvalue && value < myvalue + 5', ['value', 'index', 'myvalue'], 3)
function(value, index, myvalue) {
    return value > myvalue && value < myvalue + 5;
};

// BAD!!!!
// myvalue === index
console.log(arrayFindAllWithIndex([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], expression('value > myvalue && value < myvalue + 5', ['value', 'myvalue'], 3)));

// expression('value > myvalue && value < myvalue + 5', ['value', 'myvalue'], 3)
function(value, myvalue) {
    return value > myvalue && value < myvalue + 5;
};

from framework.

eMarek avatar eMarek commented on August 23, 2024

I barely understand the code. To much callbacks and all stuff. :)

So expression returns anonymous function which is then executed as a filter function. Filter is second parameter in arrayFindAll and arrayFindAllWithIndex functions of course. The things I do not understand are for example here:

// expression('value > myvalue && value < myvalue + 5', ['value', 'index', 'myvalue'], 3)
function(value, index, myvalue) {
    return value > myvalue && value < myvalue + 5;
};

Why are here three parameters? Is myvalue automaticaly replaced with 3 in some mysterious way?

        if (filter(value, i))
            selected.push(value);

As I understand filter use only first and second parameter.

Anyway I do not want to harass you. I will use that code without expression if you think it is better.

from framework.

petersirka avatar petersirka commented on August 23, 2024

Why are here three parameters? Is myvalue automaticaly replaced with 3 in some mysterious way?

Yes, but not replace. It's not mysterious, it's closure :-). For example:

function fn(query, properties, param1, param2, etc..) {
    return function() {
        var param = [];
        for (var i = 0; i < arguments.length; i++)
            param.push(arguments[i]);

        param.push(param1);
        param.push(param2);

        return eval('function(' + properties.join(',') + ') { return ' + query + ' };').apply(this, param);
    };
}

Anyway I do not want to harass you.

It's ok 👍

from framework.

eMarek avatar eMarek commented on August 23, 2024

I know what are closures, but I do not have so much practical experience. Actually I have gone all over again and realized that expression:

console.log(expression('value > myvalue && value < myvalue + 5', ['value', 'index', 'myvalue'], 3).toString());

returns

function () {
    var arr = [];

    for (var i = 0; i < arguments.length; i++)
        arr.push(arguments[i]);

    for (var i = 0; i < values.length; i++)
        arr.push(values[i]);

    return fn.apply(this, arr);
}

and not

function(value, index, myvalue) {
    return value > myvalue && value < myvalue + 5;
};

as I was thinking before. I am stuck again at this point.
Never mind. We are off topic and I got the answer about loading views so I am closing the issue. Thank you. :)

from framework.

petersirka avatar petersirka commented on August 23, 2024

This is full declaration of expression.
You'll understand.

var expressionCache = {};

function expression(query, params) {

    var name = params.join(',');
    var fn = expressionCache[query + '-' + name];

    if (!fn) {
        fn = eval('(function(' + name +'){' + (query.indexOf('return') === -1 ? 'return ' : '') + query + '})');
        expressionCache[query + name] = fn;
    }

    var values = [];

    for (var i = 2; i < arguments.length; i++)
        values.push(arguments[i]);

    return (function() {
        var arr = [];

        for (var i = 0; i < arguments.length; i++)
            arr.push(arguments[i]);

        for (var i = 0; i < values.length; i++)
            arr.push(values[i]);

        return fn.apply(this, arr);
    });
}

Thank you.

from framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.