Comments (7)
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.
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.
"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.
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.
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.
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.
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)
- How to ignore U.request() causing self signed certificate in certificate chain error. HOT 3
- [feature] : How to create custom node (component) using python script HOT 4
- Serve Angular components views using TotalJS HOT 3
- How can I validate with schema.define the workflow options? HOT 8
- DBMS PostgreSQL doesn't catch an error HOT 1
- How to store information from a test to reuse it HOT 4
- Please help to fix Bug in REST COMPONENT HOT 6
- How to validate the response schema from unit testing method? HOT 5
- What is substitution for CONFIG method in total.js V4? HOT 4
- AUTH global function is recieving undefined in $.params HOT 4
- Trying to get in touch regarding a security issue HOT 2
- F.snapshotstats is not a function at FrameworkCacheProto.recycle HOT 1
- Total v4 is not sending operation object when using EXEC HOT 5
- How to hide X-Powered-By for security reason? HOT 1
- Timeout Set Up HOT 4
- How to onload in totaljs 4 HOT 2
- How to set CORS for public files? HOT 3
- Exporting the JSON HOT 6
- How to load config file and read config as independent library? HOT 4
- How to custom response 408 timeout on spesific route? HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from framework.