define('app/controllers/metric_add', ['ember'],
    //
    //  Metric Add Controller
    //
    //  @returns Class
    //
    function () {

        'use strict';

        return Ember.Object.extend({


            //
            //
            //  Properties
            //
            //


            view: null,
            metrics: [],
            machine: null,
            callback: null,
            formReady: null,
            newMetric: null,


            //
            //
            //  Methods
            //
            //


            open: function (machine, callback) {
                this.clear();
                this.set('machine', machine)
                    .set('callback', callback);

                this.view.open();
                this.loadMetrics();
            },


            close: function () {
                this.clear();
                this.view.close();
            },


            add: function () {
                var that = this;
                Mist.metricsController.addMetric(
                    this.machine,
                    this.newMetric,
                    function (success, metric) {
                        if (success) {
                            if (that.callback)
                                that.callback(metric);
                            that.close();
                        }
                });
            },


            loadMetrics: function () {

                var url = '/backends/' + this.machine.backend.id +
                          '/machines/' + this.machine.id + '/metrics';

                var that = this;
                this.set('loadingMetrics', true);
                Mist.ajax.GET(url, {
                }).success(function(metrics) {
                    that._setMetrics(metrics);
                }).error(function(message) {
                    Mist.notificationController.notify(
                        'Failed to load metrics: ' + message);
                }).complete(function() {
                    that.set('loadingMetrics', false);
                });
            },


            clear: function () {
                this.view.clear();
                this.set('metrics', [])
                    .set('machine', null)
                    .set('callback', null)
                    .set('newMetric', {
                        'target': null,
                        'newName': null
                    });
            },


            //
            //
            //  Pseudo-Private Methods
            //
            //


            _metricListToObject: function (metrics) {

                var ret = new Object();
                metrics.forEach(function (metric) {
                    var prevParent = ret;
                    var list = metric.alias.split('.');
                    var lastIndex = list.length - 1;
                    list.forEach(function (subTarget, index) {

                        // Check if this property exists in the object already
                        if (!prevParent[subTarget]) {

                            // if this is the last node, append a
                            // string instead of an object
                            if (index == lastIndex)
                                prevParent[subTarget] = metric.alias;
                            else
                                prevParent[subTarget] = new Object();
                        }
                        prevParent = prevParent[subTarget];
                    });
                });
                return ret;
            },


            _metricsObjectToNodeTree: function (object) {
                var root = new Node(object, 'collectd');
            },


            _setMetrics: function (metrics) {
                Ember.run(this, function () {
                    var newMetrics = [];
                    metrics.forEach(function(metric) {
                        newMetrics.push(metric);
                    });
                    this.set('metrics', newMetrics);
                });
            },


            //
            //
            //  Observers
            //
            //


            newMetricObserver: function () {
                if (this.newMetric.target && this.newMetric.newName)
                    this.set('formReady', true);
                else
                    this.set('formReady', false);
            }.observes('newMetric.target', 'newMetric.newName', 'newMetric')
        });
    }


    function Node (dict, text) {

        var subTargets = new Array();
        for (var key in dict) {
            if (dict[key] instanceof Object)
                subTargets.push(new Node(dict[key], key));
            else
                subTargets.push({}, key);
        }

        this.text = text;
        this.subTargets = subTargets;
    };
);

@@ -126,32 +126,414 @@
  function () %7B%0A%0A
+    function Node (dict, text) %7B%0A%0A        var subTargets = new Array();%0A        for (var key in dict) %7B%0A            if (dict%5Bkey%5D instanceof Object) %7B%0A                subTargets.push(new Node(dict%5Bkey%5D, key));%0A            %7D%0A            else %7B%0A                subTargets.push(%7B%7D, key);%0A            %7D%0A        %7D%0A%0A        this.text = text;%0A        this.subTargets = subTargets;%0A    %7D;%0A%0A
         'use str
@@ -2908,32 +2908,33 @@
            //%0A%0A%0A
+%0A
             _met
@@ -4049,24 +4049,53 @@
 collectd');%0A
+                return root;%0A
             
@@ -4848,16 +4848,17 @@
 Metric')
+,
 %0A       
@@ -4863,367 +4863,15 @@
    %7D
-);
 %0A    
-%7D%0A%0A%0A    function Node (dict, text) %7B%0A%0A        var subTargets = new Array();%0A        for (var key in dict) %7B%0A            if (dict%5Bkey%5D instanceof Object)%0A                subTargets.push(new Node(dict%5Bkey%5D, key));%0A            else%0A                subTargets.push(%7B%7D, key);%0A        %7D%0A%0A        this.text = text;%0A        this.subTargets = subTargets;%0A    %7D;%0A
+)%0A%7D
 );%0A

Fix syntax errors