Angular JS - Loading modules from different files - angularjs

I'm using a few modules from http://mgcrea.github.io/angular-strap/ and a tree view: http://ngmodules.org/modules/angular.treeview and I'm having problems when loading them. For example, I've splitted treeview.js in 2 files: 1 containing the controller and another containing the directive (as I saw on some posts that it's a good practice for Angular):
app.js -> loading flexylaout, modal and a grid
var app = angular.module('app',['flexyLayout','ui.bootstrap','ngGrid']);
treeController.js
(function(){
app.controller('TreeCtrl', function($scope){
$scope.roleList1 = [
{ "roleName" : "Escapamentos e Catalisadores", "roleId" : "role1", "children" : [
{ "roleName" : "Silencioso Intermediario", "roleId" : "role12", "children" : [
{ "roleName" : "Composicao", "roleId" : "role121", "children" : [
{ "roleName" : "Material 1", "roleId" : "role1211", "children" : [] },
{ "roleName" : "Material 2", "roleId" : "role1212", "children" : [] }
]}
]}
]},
{ "roleName" : "Arquivo 1", "roleId" : "role2", "children" : [] },
{ "roleName" : "Arquivo 2", "roleId" : "role3", "children" : [] }
];
//test tree model 2
$scope.roleList2 = [
{ "roleName" : "Tubos", "roleId" : "role1", "children" : [
{ "roleName" : "Galvanizados", "roleId" : "role11", "collapsed" : true, "children" : [] },
{ "roleName" : "Conducao", "roleId" : "role12", "collapsed" : true, "children" : [
{ "roleName" : "Material 1", "roleId" : "role121", "children" : [
{ "roleName" : "Material 2", "roleId" : "role1211", "children" : [] },
{ "roleName" : "Material 3", "roleId" : "role1212", "children" : [] }
]}
]}
]}
];
});
})();
directive.js
(function(){
app.directive('treeModel',function($compile){
return{
restrict:"A",
link:function(a,g,c){
var e=c.treeModel,
h=c.nodeLabel||"label",
d=c.nodeChildren||"children",
k='<ul><li data-ng-repeat="node in '
+e+'"><i class="collapsed" data-ng-show="node.'
+d+'.length && node.collapsed" data-ng-click="selectNodeHead(node, $event)"></i><i class="expanded" data-ng-show="node.'
+d+'.length && !node.collapsed" data-ng-click="selectNodeHead(node, $event)"></i><i class="normal" data-ng-hide="node.'
+d+'.length"></i> <span data-ng-class="node.selected" data-ng-click="selectNodeLabel(node, $event)">{{node.'
+h+'}}</span><div data-ng-hide="node.collapsed" data-tree-model="node.'
+d+'" data-node-id='
+(c.nodeId||"id")+" data-node-label="
+h+" data-node-children="
+d+"></div></li></ul>";
e&&e.length&&(c.app?(a.$watch(e,function(m,b){
g.empty().html($compile(k)(a))
},
!1),
a.selectNodeHead=a.selectNodeHead||function(a,b){
b.stopPropagation&&b.stopPropagation();
b.preventDefault&&b.preventDefault();
b.cancelBubble=!0;
b.returnValue=!1;
a.collapsed=!a.collapsed
},
a.selectNodeLabel=a.selectNodeLabel||function(c,b){
b.stopPropagation&&b.stopPropagation();
b.preventDefault&&b.preventDefault();
b.cancelBubble=!0;
b.returnValue=!1;
a.currentNode&&a.currentNode.selected&&(a.currentNode.selected=void 0);
c.selected="selected";
a.currentNode=c
}):g.html($compile(k)(a)))
}
}
});
})();
What I'm willing to do is: when the page loads, a tree with that data should appear but it doesn't. Before I splitted treeview.js, the code was:
(function(){
//angular module
var myApp = angular.module('myApp', ['angularTreeview']);
//test controller
myApp.controller('myController', function($scope){
//test tree model 1
$scope.roleList1 = [
{ "roleName" : "Escapamentos e Catalisadores", "roleId" : "role1", "children" : [
{ "roleName" : "Silencioso Intermediario", "roleId" : "role12", "children" : [
{ "roleName" : "Composicao", "roleId" : "role121", "children" : [
{ "roleName" : "Material 1", "roleId" : "role1211", "children" : [] },
{ "roleName" : "Material 2", "roleId" : "role1212", "children" : [] }
]}
]}
]},
{ "roleName" : "Arquivo 1", "roleId" : "role2", "children" : [] },
{ "roleName" : "Arquivo 2", "roleId" : "role3", "children" : [] }
];
//test tree model 2
$scope.roleList2 = [
{ "roleName" : "Tubos", "roleId" : "role1", "children" : [
{ "roleName" : "Galvanizados", "roleId" : "role11", "collapsed" : true, "children" : [] },
{ "roleName" : "Conducao", "roleId" : "role12", "collapsed" : true, "children" : [
{ "roleName" : "Material 1", "roleId" : "role121", "children" : [
{ "roleName" : "Material 2", "roleId" : "role1211", "children" : [] },
{ "roleName" : "Material 3", "roleId" : "role1212", "children" : [] }
]}
]}
]}
];
//roleList1 to treeview
$scope.roleList = $scope.roleList;
});
})();
(function(l){l.module("angularTreeview",[]).directive("treeModel",function($compile)
{return{restrict:"A",link:function(a,g,c)
{var e=c.treeModel,h=c.nodeLabel||"label",d=c.nodeChildren||"children",
k='<ul><li data-ng-repeat="node in '+e+'">
<i class="collapsed" data-ng-show="node.'+d+'.
length && node.collapsed" data-ng-click="selectNodeHead(node, $event)">
</i><i class="expanded" data-ng-show="node.'+d+'.length && !node.collapsed"
data-ng-click="selectNodeHead(node, $event)"></i>
<i class="normal" data-ng-hide="node.'+d+'.length"></i>
<span data-ng-class="node.selected" data-ng-click="selectNodeLabel(node, $event)">
{{node.'+h+'}}</span>
<div data-ng-hide="node.collapsed" data-tree-model="node.'+d+'
" data-node-id='+(c.nodeId||"id")+" data-node-label="+h+" data-node-children="+d+">
</div></li></ul>";
e&&e.length&&(c.angularTreeview?(a.$watch(e,function(m,b){
g.empty().html($compile(k)(a))},!1),
a.selectNodeHead=a.selectNodeHead||function(a,b){b.stopPropagation&&
b.stopPropagation();b.preventDefault&&b.preventDefault();b.cancelBubble=
!0;b.returnValue=!1;
a.collapsed=!a.collapsed},b.selectNodeLabel=a.selectNodeLabel||function(c,b){ b.stopPropagation&&b.stopPropagation();
b.preventDefault&&b.preventDefault();b.cancelBubble=!0;b.returnValue=!1;
a.currentNode&&a.currentNode.selected&& (a.currentNode.selected=void0;
c.selected="selected";a.currentNode=c}):g.html($compile(k)(a)))}}})})(angular);
everything was working fine before I spllited that file (but I was testing with 1 only module, in other words, no flexy layout, grid, etc. only tree view)
I appreciate any tips/suggestions..
Lucas.

There should only be one module declared to manage the whole page and that module will have same name as ng-app. All the modules you inject should load in page before you try to inject them...can't inject what doesn't exist yet
You inject all other dependent modules in dependency array for the main ng-app page module.
Once you have initialized your 'ng-app` module, use the variable you assign it to to create controllers/directives/services etc.
You have one module you create as app and another as myApp. To use both you need to inject the one that doesn't match ng-app into the one that does

Related

Fetch a field from array MongoDB Meteor

Hi I have the following collection structure:
{
"_id" : "HZw2ktDPm6EWnGaFt",
"createdAt" : ISODate("2017-04-16T17:40:59.055Z"),
"pollName" : "",
"entryOwner" : "eHPeQPMd94MQFNXmg",
"question" : [
{
"name" : "Question 1",
"questionId" : "sdPzbn9SWjE46HtM2"
},
{
"name" : "Question 2",
"questionId" : "vpMrpbJ2LZKMLEYKe"
}
],
"sharedWith" : [
{
"id" : "jjX5EDdqMtcyQwd6h",
"name" : "person 1",
"votes" : 0
},
{
"id" : "b3Ctr6LFZMd9smd4B",
"name" : "person 2",
"votes" : 0
}
],
"voters" : [
{
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "vpMrpbJ2LZKMLEYKe",
"optionId" : "EKnYKXEFBWnr4hnCP",
"peopleId" : "b3Ctr6LFZMd9smd4B"
},
{
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "vpMrpbJ2LZKMLEYKe",
"optionId" : "EKnYKXEFBWnr4hnCP",
"peopleId" : "jjX5EDdqMtcyQwd6h"
},
{
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "sdPzbn9SWjE46HtM2",
"optionId" : "rjYLitibXDJjGYKM7",
"peopleId" : "b3Ctr6LFZMd9smd4B"
},
{
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "Q6JiaGFAi2LRHS7GQ",
"optionId" : "wFoduKp23cSYJJG9i",
"peopleId" : "b3Ctr6LFZMd9smd4B"
}
]
}
I would like to get the value of Voters.peopleId by using these values.
"voterId" : "eHPeQPMd94MQFNXmg",
"questionId" : "vpMrpbJ2LZKMLEYKe",
"optionId" : "EKnYKXEFBWnr4hnCP",
I tried this and it didnt work it returns the whole document but what i want as a return is just one field:
var getPeopleId = Polls.findOne({
_id:this.props.poll._id}, {"voters": {
$elemMatch :{voterId:Meteor.userId(),questionId:selectedQuestionId,optionId:selectedOptionId}}})
Many thanks
You've got $elemMatch in the projection (2nd param) instead of the query (1st param). You also need to project the result to only include the first match. Try:
const poll = Polls.findOne(
{
_id:this.props.poll._id,
voters: {
$elemMatch: {
voterId: Meteor.userId(),
questionId: selectedQuestionId,
optionId: selectedOptionId
}
},{
'voters.$': 1
});
// guard against missing keys or no results
const peopleId = poll && poll.voters && poll.voters.peopleId;

Why I can't get the full document form array?

I have this document in stored in my collection:
{ "_id" : ObjectId("5707b95b8415b224a48a0b2d"),
"companyId" : ObjectId("570269639caabe24e4e4043e"),
"descriptions" : [
{ "id" : ObjectId("5707b95b8415b224a48a0b2a"), "description" : "test" },
{ "id" : ObjectId("570cd8164fff3a20f88c0dc9"), "description" : "test1" },
{ "id" : ObjectId("570ce6ba4fff3a052c8c570f"), "description" : "etr" },
{ "id" : ObjectId("570cf1b64fff3a1a14d71716"), "description" : "43" },
{ "id" : ObjectId("570cf1b64fff3a1a14d71717"), "description" : "43" },
{ "id" : ObjectId("570cf1b64fff3a1a14d71719"), "description" : "345" }
],
"options" : [
{ "descriptionId" : ObjectId("5707b95b8415b224a48a0b2a"), "description" : "test" },
{ "descriptionId" : ObjectId("5707b95b8415b224a48a0b2a"), "description" : "test1" }
]
}
Now I'm trying to get the objects from the options array that are matching the descriptionId and here is how I'm doing it
db.CustomFields.find({companyId: ObjectId("570269639caabe24e4e4043e")},{"options.descriptionId": ObjectId("5707b95b8415b224a48a0b2a")})
But the result contains only the descriptionId - the description property is missing.
here is how the result looks like:
{ "_id" : ObjectId("5707b95b8415b224a48a0b2d"),
"options" : [
{ "descriptionId" : ObjectId("5707b95b8415b224a48a0b2a") },
{ "descriptionId" : ObjectId("5707b95b8415b224a48a0b2a") }
]
}
Why my query is not returning the full document from the array, but only a part of it? Can you give me a push?
EDIT
This is what I'm expecting to get from the query
{ "_id" : ObjectId("5707b95b8415b224a48a0b2d"),
"options" : [
{ "descriptionId" : ObjectId("5707b95b8415b224a48a0b2a", "description" : "test") },
{ "descriptionId" : ObjectId("5707b95b8415b224a48a0b2a", "description" : "test1") }
]
}
You need to include the other query with "options.descriptionId" together with the companyId query and use projection to return just the array you want.
The following shows this:
db.customFields.find(
{
"companyId": ObjectId("570269639caabe24e4e4043e"),
"options.descriptionId": ObjectId("5707b95b8415b224a48a0b2a")
},
{ "options": 1 }
);
Output
{
"_id" : ObjectId("5707b95b8415b224a48a0b2d"),
"options" : [
{
"descriptionId" : ObjectId("5707b95b8415b224a48a0b2a"),
"description" : "test"
},
{
"descriptionId" : ObjectId("5707b95b8415b224a48a0b2a"),
"description" : "test1"
}
]
}
Try this
db.CustomFields.find({companyId: ObjectId("570269639caabe24e4e4043e"),"options.descriptionId": ObjectId("5707b95b8415b224a48a0b2a")})

how to get selected node value for treeview using click event in angularjs directives

i am using directive concept in angularjs to display selected node value for tree view using click event function in angularjs.below is my sample code
Tree.html:
<div
data-angular-treeview="true"
data-tree-model="roleList"
data-node-id="roleId"
data-node-label="roleName"
data-node-children="children"
data-ng-click="selectNode(roleList[1])"
data-node-children="children">
</div>
treeviewcontroller.js:
$scope.roleList1 = [
{ "roleName" : "User", "roleId" : "role1", "children" : [
{ "roleName" : "subUser1", "roleId" : "role11", "children" : [] },
{ "roleName" : "subUser2", "roleId" : "role12", "children" : [
{ "roleName" : "subUser2-1", "roleId" : "role121", "children" : [
{ "roleName" : "subUser2-1-1", "roleId" : "role1211", "children" : [] },
{ "roleName" : "subUser2-1-2", "roleId" : "role1212", "children" : [] }
]}
]}
]},
{ "roleName" : "Admin", "roleId" : "role2", "children" : [] },
{ "roleName" : "Guest", "roleId" : "role3", "children" : [] }
];
Treeview.js:
scope.selectNode = function(val)
{
alert(val.roleName);
}
output:
user
subuser1
subuser1-1
Admin
subadmin1
from this output in alert place 'Admin' will be dispalyed by click on Admin node.but i want to display dynamically selected node value in click event function.please suggest me how to do this.
Thanks
In order to dynamically select node of the tree I did following:
Let's say you want to select first (top, [0]) element of your tree.
so first add data-tree-id="myTreeId" to your HTML:
<div
data-angular-treeview="true"
data-tree-model="roleList"
data-node-id="roleId"
data-node-label="roleName"
data-node-children="children"
data-ng-click="selectNode(roleList[1])"
data-node-children="children"
data-tree-id="myTreeId">
</div>
than in Controller:
$scope.roleList1[0].selected = "selected";
$scope.myTreeId.currentNode = $scope.roleList1[0];
In Tree.html :
Add line :
data-tree-id="mytree"
Modify data-ng-click event :
data-ng-click="selectNode(mytree.currentNode.roleName)"
In Treeview.js :
scope.selectNode = function(val) { alert(val); }

Mongodb - moving a field in an array to a new array in another parent

I have the following data scheme in mongodb database.
Due to a user interaction an entry can be moved from CAT_A to CAT_B, and the angularjs model changes appropriately.
[
{
"_id":"537f4407cb8a077d396bd73e",
"cat":"CAT_A",
"ntype":"category",
"entries":[
{
"title":"111",
"content":"Content One",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd741"
},
{
"title":"222",
"content":"Content Two",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd740"
},
{
"title":"333",
"content":"Content Three",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd73f"
}
]
},
{
"_id":"537f4407cb8a077d396bd742",
"cat":"CAT_B",
"ntype":"category",
"entries":[
{
"title":"444",
"content":"Content Four",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd745"
},
{
"title":"555",
"content":"Content Five",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd744"
},
{
"title":"666",
"content":"Content Six",
"ntype":"entry",
"_id":"537f4407cb8a077d396bd743"
}
]
}
]
How do I save this new model to the mongo database, or really what is the best way to handle this?
Things I've though about doing:
Simply remove all the categories involved(there will be more than 2) from the database, and recreate them from the new model. This seems inefficient, also the content field may contain larger amounts of data, which makes http requests expensive.
Same as 1, but leave the 'content' out of the schema, create a new collection for content only, and somehow link that to the entry ID.
Pull an entry from CAT_A and push to CAT_B, have struggled getting this working and what if I wanted to keep the index position as in the model? ie if I wanted to move entry 6 in CAT_B to between entry 1 and 2 in CAT_A?
cheers
EDIT new schemas:
var CatSchema = new Schema({
name : String,
ntype : String,
incentries: {
ntype : String,
entry_id : { type: Schema.Types.ObjectId, ref: 'Entry' },
entry_title : String
}
});
var EntrySchema = new Schema({
cat : { type: Schema.ObjectId, ref: 'Cat' },
title : String,
content : String,
});
and the code:
exports.editCat = function (req, res) {
Cat.update({_id: req.body.old},
{$pull: {'incentries': {'entry_id': req.body.entry}}},
function (err, data) {
});
Cat.update({_id: req.body.new},
{$addToSet: { incentries : {'entry_id': req.body.entry, 'entry_title': req.body.entryTitle, ntype: 'entry' }}},
function (err, data) {
});
};
Use $pull and $push:
>db.elements.findOne()
{
"_id" : ObjectId("537f6ddcd66d3634fe5963f6"),
"arr" : [
{
"_id" : "537f4407cb8a077d396bd73e",
"cat" : "CAT_A",
"ntype" : "category",
"entries" : [
{
"title" : "111",
"content" : "Content One",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd741"
},
{
"title" : "222",
"content" : "Content Two",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd740"
},
{
"title" : "333",
"content" : "Content Three",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd73f"
}
]
},
{
"_id" : "537f4407cb8a077d396bd742",
"cat" : "CAT_B",
"ntype" : "category",
"entries" : [
{
"title" : "444",
"content" : "Content Four",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd745"
},
{
"title" : "555",
"content" : "Content Five",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd744"
},
{
"title" : "666",
"content" : "Content Six",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd743"
}
]
}
]
}
> db.elements.update({"_id" : ObjectId("537f6ddcd66d3634fe5963f6")},{$pull:{"arr.0.entries":entry},$push:{"arr.1.entries":entry}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>db.elements.findOne()
{
"_id" : ObjectId("537f6ddcd66d3634fe5963f6"),
"arr" : [
{
"_id" : "537f4407cb8a077d396bd73e",
"cat" : "CAT_A",
"ntype" : "category",
"entries" : [
{
"title" : "222",
"content" : "Content Two",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd740"
},
{
"title" : "333",
"content" : "Content Three",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd73f"
}
]
},
{
"_id" : "537f4407cb8a077d396bd742",
"cat" : "CAT_B",
"ntype" : "category",
"entries" : [
{
"title" : "444",
"content" : "Content Four",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd745"
},
{
"title" : "555",
"content" : "Content Five",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd744"
},
{
"title" : "666",
"content" : "Content Six",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd743"
},
{
"title" : "111",
"content" : "Content One",
"ntype" : "entry",
"_id" : "537f4407cb8a077d396bd741"
}
]
}
]
}
But you will have to know where CAT_A and CAT_B are within the array (position 0 and position 1)
You could also try a schema like this:
{
"_id" : ObjectId("537f6dddd66d3634fe5963f7"),
"categories" : {
CAT_A: {
"_id" : "537f4407cb8a077d396bd73e",
"cat" : "CAT_A",
"ntype" : "category",
"entries" : [...]
},
CAT_B:{
"_id" : "537f4407cb8a077d396bd742",
"cat" : "CAT_B",
"ntype" : "category",
"entries" : [...]
}
}
}
So you update query would finally be:
> db.elements.update({"_id" : ObjectId("537f6ddcd66d3634fe5963f6")},{$pull:{"categories.CAT_B.entries":entry},$push:{"categories.CAT_A.entries":entry}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
As long as you "cat" field is unique within a single document this other approach seems better to me.

Mongodb - How to update a nested Array

I have to update an Array nested in another Array.
Is it possible to do that with the $addtoset operator?
Here is my document.
I have a library wich has an Array of authors who have an Array of Books.
In this exemple, i'd like to add a new category to the book id 1 of the author id 1
How can i do that ?
{
"id" : NumberLong(666),
"library" : [
{
"id" : NumberLong(8888),
"author" : [
{
"id" : NumberLong(1),
"books" : [
{
"id":1,
"title" : "plop",
"category" : ["horror"],
"isbn" : 12345
},
{
"id":2,
"title" : "plup",
"category" : ["comics"],
"isbn" : 6789
}
]
},
{
"id" : NumberLong(2),
"books" : [
{
"id":3,
"title" : "blop",
"category" : ["horror"],
"isbn" : 96325
},
{
"id":4,
"title" : "blup",
"category" : ["comics"],
"isbn" : 74125
}
]
}
]
}
]
}
I tried this :
db.library.update(
{"id":666,"author.id":1,"books.id":1},
{"$addToSet": {"author.$.books.category": "humour" }}
)
But it doesn't works
How does it works ?
thanks a lot
db.library.update(
{"id":666,"library.author.id":1,"library.author.books.id":1},
{"$addToSet": {"library.0.author.0.books.$.category": "Book"}}
)

Resources