Skip to content

Commit

Permalink
Add viewmodel boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
mdarens committed Feb 17, 2014
1 parent fc6c208 commit 18c9d8b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var
data = getGraphData(),
nodes = data.nodes,
links = data.links;

$.each(nodes, function(index, node) {
node.index = index;
});

function getNodeById(id) {
return _.find(nodes, { id : id });
}

function getNodeByIndex(index) {
return nodes[index];
}

function getDependents(node, links) {
return $.map(_.where(links, { target: node.index }), function(item){
return getNodeByIndex(item.source);

});
}

var modules = $.map(nodes, function(node){
return {
'id': node.id,
'dependentCount': getDependents(node,links).length,
'dependents': getDependents(node,links),
'isOpen': ko.observable(false)
};
});

modules = _.sortBy(modules, 'dependentCount').reverse();

var vm = {};

vm.modules = ko.observableArray(modules);

ko.applyBindings(vm);

0 comments on commit 18c9d8b

Please sign in to comment.