Skip to content

Instantly share code, notes, and snippets.

@justinobney
Last active March 24, 2016 20:41
Show Gist options
  • Save justinobney/3d9df40f64a7a40c4908 to your computer and use it in GitHub Desktop.
Save justinobney/3d9df40f64a7a40c4908 to your computer and use it in GitHub Desktop.

Revisions

  1. justinobney revised this gist Jul 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion $watching.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ app.controller('Ctrl1', function($scope, DataFactory) {

    // watch the collection for changes
    $scope.$watch(watchSource, function(current, previous){
    this.items = current
    this.items = current;
    });

    function watchSource(){
  2. justinobney revised this gist Jul 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion $watching.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ app.controller('Ctrl1', function($scope, DataFactory) {
    this.items = DataFactory.items;

    // watch the collection for changes
    $scope.$watch(warchSource, function(current, previous){
    $scope.$watch(watchSource, function(current, previous){
    this.items = current
    });

  3. justinobney revised this gist Jul 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion collectionreset.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    svc.getDataStream = function() {
    return $q.when(fakeHttpResponse())
    return $q.when(fakeData)
    .then(function(data) {
    // here we are clearly reseting the data
    // to the response of the call.. Why doesn't
  4. justinobney revised this gist Jul 3, 2014. 4 changed files with 9 additions and 14 deletions.
    6 changes: 2 additions & 4 deletions $watching.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    var app1 = angular.module('app1', []);

    app1.controller('Ctrl1', function($scope, DataFactory) {
    app.controller('Ctrl1', function($scope, DataFactory) {
    // bind the controller property to the service collection
    this.items = DataFactory.items;

    @@ -14,7 +12,7 @@ app1.controller('Ctrl1', function($scope, DataFactory) {
    }
    });

    app1.factory('DataFactory', function($q, $timeout) {
    app.factory('DataFactory', function($q, $timeout) {
    var svc = {};
    svc.items = [];

    7 changes: 2 additions & 5 deletions blog-ng-list-services1.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,4 @@
    // create the application
    var app1 = angular.module('app1', []);

    app1.controller('Ctrl1', function(DataFactory) {
    app.controller('Ctrl1', function(DataFactory) {
    // bind the controller property to the service collection
    this.items = DataFactory.items;

    @@ -15,7 +12,7 @@ app1.controller('Ctrl1', function(DataFactory) {
    });

    // sample "service" for getting data
    app1.factory('DataFactory', function($q, $timeout) {
    app.factory('DataFactory', function($q, $timeout) {
    var svc = {};
    svc.items = [];

    2 changes: 1 addition & 1 deletion blog-ng-list-services2.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    app1.controller('Ctrl1', function(DataFactory) {
    app.controller('Ctrl1', function(DataFactory) {
    // bind the controller property to the service collection
    this.items = DataFactory.items;

    8 changes: 4 additions & 4 deletions final_structure.js
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,19 @@
    var app1 = angular.module('app1', []);
    var app = angular.module('app', []);

    app1.controller('Ctrl1', function(DataFactory) {
    app.controller('Ctrl1', function(DataFactory) {
    this.items = DataFactory.items;

    DataFactory.getDataStream();
    });

    app1.controller('Ctrl2', function($timeout, DataFactory) {
    app.controller('Ctrl2', function($timeout, DataFactory) {
    // when this eventually fires and gets *remote* data again
    // our other controller will automatically sync up
    // without the need for the $watch function
    $timeout(DataFactory.getDataStream, 2000);
    });

    app1.factory('DataFactory', function($q) {
    app.factory('DataFactory', function($q) {
    var svc = {};
    svc.items = [];

  5. justinobney revised this gist Jul 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion final_structure.js
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ app1.controller('Ctrl2', function($timeout, DataFactory) {
    $timeout(DataFactory.getDataStream, 2000);
    });

    app1.factory('DataFactory', function($q, $timeout) {
    app1.factory('DataFactory', function($q) {
    var svc = {};
    svc.items = [];

  6. justinobney revised this gist Jul 3, 2014. 3 changed files with 39 additions and 22 deletions.
    22 changes: 22 additions & 0 deletions $watching.js
    Original file line number Diff line number Diff line change
    @@ -12,4 +12,26 @@ app1.controller('Ctrl1', function($scope, DataFactory) {
    function watchSource(){
    return DataFactory.items;
    }
    });

    app1.factory('DataFactory', function($q, $timeout) {
    var svc = {};
    svc.items = [];

    svc.getDataStream = function() {

    var fakeData = [
    { id: 1, name: 'name 1' },
    { id: 2, name: 'name 2' },
    { id: 4, name: 'name 4' }
    ];

    // using $q to fake async data grab
    return $q.when(fakeData)
    .then(function(data) {
    svc.items = data;
    });
    };

    return svc;
    });
    19 changes: 8 additions & 11 deletions blog-ng-list-services1.js
    Original file line number Diff line number Diff line change
    @@ -20,22 +20,19 @@ app1.factory('DataFactory', function($q, $timeout) {
    svc.items = [];

    svc.getDataStream = function() {

    var fakeData = [
    { id: 1, name: 'name 1' },
    { id: 2, name: 'name 2' },
    { id: 4, name: 'name 4' }
    ];

    // using $q to fake async data grab
    return $q.when(fakeHttpResponse())
    return $q.when(fakeData)
    .then(function(data) {
    svc.items = data;
    });
    };

    return svc;

    // ignore for demo purposes only.
    function fakeHttpResponse() {
    return [
    { id: 1, name: 'name 1' },
    { id: 2, name: 'name 2' },
    { id: 3, name: 'name 3' },
    { id: 4, name: 'name 4' }
    ];
    }
    });
    20 changes: 9 additions & 11 deletions final_structure.js
    Original file line number Diff line number Diff line change
    @@ -18,22 +18,20 @@ app1.factory('DataFactory', function($q, $timeout) {
    svc.items = [];

    svc.getDataStream = function() {
    return $q.when(fakeHttpResponse())

    var fakeData = [
    { id: 1, name: 'name 1' },
    { id: 2, name: 'name 2' },
    { id: 4, name: 'name 4' }
    ];

    // using $q to fake async data grab
    return $q.when(fakeData)
    .then(function(data) {
    // this is the magic
    angular.copy(data, svc.items);
    });
    };

    return svc;

    // ignore for demo purposes only.
    function fakeHttpResponse() {
    return [
    { id: 1, name: 'name 1' },
    { id: 2, name: 'name 2' },
    { id: 3, name: 'name 3' },
    { id: 4, name: 'name 4' }
    ];
    }
    });
  7. justinobney revised this gist Jul 3, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions final_structure.js
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,13 @@ app1.controller('Ctrl1', function(DataFactory) {
    DataFactory.getDataStream();
    });

    app1.controller('Ctrl2', function($timeout, DataFactory) {
    // when this eventually fires and gets *remote* data again
    // our other controller will automatically sync up
    // without the need for the $watch function
    $timeout(DataFactory.getDataStream, 2000);
    });

    app1.factory('DataFactory', function($q, $timeout) {
    var svc = {};
    svc.items = [];
  8. justinobney revised this gist Jul 3, 2014. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions $watching.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    var app1 = angular.module('app1', []);

    app1.controller('Ctrl1', function($scope, DataFactory) {
    // bind the controller property to the service collection
    this.items = DataFactory.items;

    // watch the collection for changes
    $scope.$watch(warchSource, function(current, previous){
    this.items = current
    });

    function watchSource(){
    return DataFactory.items;
    }
    });
  9. justinobney revised this gist Jul 1, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions final_structure.js
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ app1.factory('DataFactory', function($q, $timeout) {
    svc.getDataStream = function() {
    return $q.when(fakeHttpResponse())
    .then(function(data) {
    // this is the magic
    angular.copy(data, svc.items);
    });
    };
  10. justinobney revised this gist Jul 1, 2014. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions final_structure.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    var app1 = angular.module('app1', []);

    app1.controller('Ctrl1', function(DataFactory) {
    this.items = DataFactory.items;

    DataFactory.getDataStream();
    });

    app1.factory('DataFactory', function($q, $timeout) {
    var svc = {};
    svc.items = [];

    svc.getDataStream = function() {
    return $q.when(fakeHttpResponse())
    .then(function(data) {
    angular.copy(data, svc.items);
    });
    };

    return svc;

    // ignore for demo purposes only.
    function fakeHttpResponse() {
    return [
    { id: 1, name: 'name 1' },
    { id: 2, name: 'name 2' },
    { id: 3, name: 'name 3' },
    { id: 4, name: 'name 4' }
    ];
    }
    });
  11. justinobney revised this gist Jul 1, 2014. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions collectionreset.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    svc.getDataStream = function() {
    return $q.when(fakeHttpResponse())
    .then(function(data) {
    // here we are clearly reseting the data
    // to the response of the call.. Why doesn't
    // it just databind?
    svc.items = data;
    });
    };
  12. justinobney revised this gist Jul 1, 2014. 2 changed files with 8 additions and 1 deletion.
    1 change: 0 additions & 1 deletion blog-ng-list-services1.js
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,6 @@ app1.controller('Ctrl1', function(DataFactory) {
    // sample "service" for getting data
    app1.factory('DataFactory', function($q, $timeout) {
    var svc = {};

    svc.items = [];

    svc.getDataStream = function() {
    8 changes: 8 additions & 0 deletions blog-ng-list-services2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    app1.controller('Ctrl1', function(DataFactory) {
    // bind the controller property to the service collection
    this.items = DataFactory.items;

    // but wouldn't it be so much better
    // to just call it and let it work
    DataFactory.getDataStream();
    });
  13. justinobney revised this gist Jul 1, 2014. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions blog-ng-list-services1.js
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,7 @@ app1.controller('Ctrl1', function(DataFactory) {
    // invoke the call to get data
    DataFactory
    .getDataStream()
    .then(function(data) {
    // update the service collection
    DataFactory.items = data;
    .then(function() {
    // update the controller collection property
    this.items = DataFactory.items;
    }.bind(this));
    @@ -26,7 +24,7 @@ app1.factory('DataFactory', function($q, $timeout) {
    // using $q to fake async data grab
    return $q.when(fakeHttpResponse())
    .then(function(data) {
    return data;
    svc.items = data;
    });
    };

  14. justinobney revised this gist Jul 1, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions blog-ng-list-services1.js
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,25 @@
    // create the application
    var app1 = angular.module('app1', []);

    app1.controller('Ctrl1', function(DataFactory) {
    // bind the controller property to the service collection
    this.items = DataFactory.items;

    // invoke the call to get data
    DataFactory
    .getDataStream()
    .then(function(data) {
    // update the service collection
    DataFactory.items = data;
    // update the controller collection property
    this.items = DataFactory.items;
    }.bind(this));
    });

    // sample "service" for getting data
    app1.factory('DataFactory', function($q, $timeout) {
    var svc = {};

    svc.items = [];

    svc.getDataStream = function() {
  15. justinobney revised this gist Jul 1, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions blog-ng-list-services1.js
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ app1.controller('Ctrl1', function(DataFactory) {

    app1.factory('DataFactory', function($q, $timeout) {
    var svc = {};
    svc.items = [];

    svc.getDataStream = function() {
    // using $q to fake async data grab
  16. justinobney created this gist Jul 1, 2014.
    36 changes: 36 additions & 0 deletions blog-ng-list-services1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    var app1 = angular.module('app1', []);

    app1.controller('Ctrl1', function(DataFactory) {
    this.items = DataFactory.items;

    DataFactory
    .getDataStream()
    .then(function(data) {
    DataFactory.items = data;
    this.items = DataFactory.items;
    }.bind(this));
    });

    app1.factory('DataFactory', function($q, $timeout) {
    var svc = {};

    svc.getDataStream = function() {
    // using $q to fake async data grab
    return $q.when(fakeHttpResponse())
    .then(function(data) {
    return data;
    });
    };

    return svc;

    // ignore for demo purposes only.
    function fakeHttpResponse() {
    return [
    { id: 1, name: 'name 1' },
    { id: 2, name: 'name 2' },
    { id: 3, name: 'name 3' },
    { id: 4, name: 'name 4' }
    ];
    }
    });