ngShadow Attempt

by langdonx

HTML

<div ng-app="app" ng-controller="TestCtrl">
    <div class="main">
        Main View:
        <pre>{{items|json}}</pre>
    </div>
    <div class="editor">
        Editor Controls:
        <br/>
        <br/>
        <dir1 ng-repeat="item in items" shadow="item" test="1">
            <input type="text" ng-model="shadow.name" />
            <input type="text" ng-model="shadow.info" />
            <button ng-click="commit()">commit</button>
            <br/>
            <br/>
        </dir1>
    </div>
</div>

CSS

.main {
    float: left;
}

.editor {
    float: left;
}

JavaScript

var app = angular.module('app', []);

app.controller('TestCtrl', function ($scope) {
    console.log('TestCtrl', $scope.$id);

    $scope.items = [{
        id: 1,
        name: 'test',
        info: 'stuff'
    }, {
        id: 2,
        name: 'asdf',
        info: 'xyz'
    }, {
        id: 3,
        name: 'qwerty',
        info: 'iii '
    }];
});

app.directive('dir1', function () {
    return {
        restrict: 'E',
        scope: true,
        link: function (scope, element, attrs) {
            console.log('dir1', scope.$id);

            var original = scope.$parent[attrs.shadow];

            scope.shadow = angular.copy(original);
            scope.commit = function () {
                // TODO how do you call this from an $http callback?
                var key;

                for (key in original) {
                    original[key] = scope.shadow[key];
                }
            };
        }
    };
});