0

I'm an angular newb. I've followed this tutorial http://www.bradoncode.com/tutorials/learn-mean-stack-tutorial/ and have slightly modified things (I have items instead of categories). Basically I've created a MEAN app, with two models, users and items. I would like to add a property to the items model called "owner" which stores the username of the user who created the model. This isn't too hard, but a problem arises if the user changes their username - the "owner" property doesn't change in the item model instances created by the user. From what I gather there are ways of syncing data between views/models/controllers, but how would I sync data between different model instances of different models. Any help is appreciated, thanks.

Here is item.client.controller.js

'use strict';

// Items controller
angular.module('items').controller('ItemsController', ['$scope','$stateParams', '$location', 'Authentication', 'Items',
function($scope, $stateParams, $location, Authentication, Items) {
    $scope.authentication = Authentication;
    $scope.currentPage = 1;
    $scope.pageSize = 10;
    $scope.offset = 0;

   // Page changed handler
   $scope.pageChanged = function() {
        $scope.offset = ($scope.currentPage - 1) * $scope.pageSize;
   };

    // Create new Item
    $scope.create = function() {
        // Create new Item object
        var item = new Items ({
            name: this.name,
            description: this.description,
            owner: $scope.authentication.user.username
        });

Here is items.client.services.js 'use strict';

//Items service used to communicate Items REST endpoints
angular.module('items').factory('Items', ['$resource',
function($resource) {
    return $resource('items/:itemId', { itemId: '@_id'
    }, {
        update: {
            method: 'PUT'
        }
    });
    }
]);

And users.client.services.js

'use strict';

// Users service used for communicating with the users REST endpoint
angular.module('users').factory('Users', ['$resource',
function($resource) {
    return $resource('users', {}, {
        update: {
            method: 'PUT'
        }
    });
   }
]);
Forest Hughes
  • 143
  • 1
  • 3
  • 12
  • one way of syncing data is through events. See [link](http://stackoverflow.com/questions/14502006/working-with-scope-emit-and-on) – John Ephraim Tugado Jan 21 '16 at 03:22
  • can you show your code here? – rkalita Jan 21 '16 at 03:22
  • All of the relevant code is here (this is from the creator of the tutorial). https://github.com/bbraithwaite/NorthwindNode The relevant models (category, and user) are in app/models, the server controllers are in app/controllers, the services and client controllers are in public/modules/categories and public/modules/users. I'm very new to Angular so I'm not even sure exactly where this code would go. So far I've mostly just changed the actual category model to add the "owner" property, and the client category controller to fill the category. – Forest Hughes Jan 21 '16 at 03:28
  • You should provide at least some code, if in the future that link dies this will not provide any help for anyone and chances are you will get lots of down votes. – Daniel Tate Jan 21 '16 at 03:36
  • Imho the logic of syncing the data between models should not happen in a model. But the link provided by @JohnEphraimTugado should work for you. You could broadcast an "usernameChanged"-event – hering Mar 20 '17 at 10:41

0 Answers0