Comments (5)
Hy,
thank you for the feedback.
Good idea, I'll try to do it this week.
from mongoose-materialized.
Hello,
I had a little free time, so I made the requested function.
You can use npm tool for update and checkout the changelog and API reference.
https://github.com/janez89/mongoose-materialized#changelog
from mongoose-materialized.
Haha that was pretty damn quick! Awesome job man, Will check them out!
from mongoose-materialized.
Just tested the functions, really impressed with the quality work! Something did surprise me though. If you use
Cat.GetArrayTree('elemt ID', function (err, tree) {
// ...
});
It returns the tree correctly but if you let's say input a id of 10 and 10 has a few parents it will also include parents, is this working as intended? (the way I did it now was with a findById and then ask the tree which returned only the tree starting from that ID).
Anyhow I am so thankful for the (quick) fulfilment of this feature! I also made a function that is able to import a arrayTree and write it to the db, maybe it is not perfectly written but I will leave it here if people ever need a feature that's similar.
importDataArray: function(db, json, name, callback) {
var self = this;
var counter = 0;
json.forEach(function(node) {
counter++;
self.importDataObj(db, node, name, function(err) {
if (err) {
callback(err);
} else {
counter--;
if (counter === 0) {
callback();
}
}
});
});
},
importDataObj: function(db, node, name, callback) {
var self = this;
var counter = 1;
var callbackCheck = false;
if (name) {
node.data.name = name;
}
saveNode(node, null);
function saveNode(json, parentId) {
counter--;
if(json.children !== undefined) {
counter += json.children.length;
}
var newNode = {};
newNode = extend(true, newNode, json);
newNode.parentId = parentId;
delete newNode._id;
delete newNode.path;
delete newNode.children;
self.saveNode(db, newNode, function(err, result) {
if (err) {
console.log(err);
callback(err);
} else {
if (json.children !== undefined) {
for (var i = 0; i < json.children.length; i++) {
saveNode(json.children[i], result._id);
}
}
}
if (counter === 0 && !callbackCheck) {
callbackCheck = true;
callback();
}
});
}
}
from mongoose-materialized.
Hi janez89 and Lailend,
I try to limit result of query
Cat.GetArrayTree('elemt ID', function (err, tree) {
// ...
});
and have array with only one level of children node
{
"items": [
{
"id": "53a97576bcdd8dbc905727ac",
"text": "Cars",
"children": [
{
"id": "543c35b78521e35432b790c6",
"text": "VW"
},
{
"id": "53a9958cbcdd8dbc905727ad",
"text": "Volvo"
}
]
}
]
}
and function to return this
Category.model.findOne({_id: nodeID}, function(err, node){
node.getArrayTree(function(err, childs) {
sendResponse({
items: childs.map(function(i){
return {
id: i.id
,text: i.name
,children: i.children.map(function(j){
return {
id: j.id
,text: j.name
}
})
}
})
})
})
How reduce resultset of function GetArrayTree to some fields and have all children levels?
Plase help.
Andrzej
from mongoose-materialized.
Related Issues (20)
- Retrieve all elements as "tree view" HOT 1
- .save does not update all path HOT 2
- Populating references HOT 1
- err returning as an array
- ToArrayTree doesn't sort children nodes
- GetChildren not checking if doc is returned
- Wwhy sorting is not working in getArrayTree HOT 1
- Modify the hierarchy structure
- GetArrayTree field selection hides children HOT 3
- Possibility to duplicate a tree? HOT 2
- getChildrenTree returns wrong tree with sort option
- MongoDB 3.2 Limit / Skip not working HOT 1
- Project development seems to have stalled HOT 2
- ES6 Promise compatibility HOT 1
- setParent only updates the parentId, not the path HOT 1
- Sort is not working... HOT 1
- Discriminator support
- Can't use ES6 promise with mpromise style constructor HOT 5
- Doesn't work with Mongoose 5
- Saving a parentId that points to non-existing document results in an uncontrolled crash
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 mongoose-materialized.