angularjs - Sharing data from multiple instances of a directive -


i have custom directive re-usable throughout application. directive uses ui-grid , i'm trying determine "angular way" of allowing access grid api anywhere in application.

if single instance, i'd use service share data across controllers:

var attachments = angular.module('attachments', ['ui.grid']);  // accessible of controllers attachments.factory('attachments', function() {     return {}; });  attachments.directive('attachments', function() {     return {         restrict: 'e',         templateurl: 'attachments.html', // template has ui-grid, among other elements         controller: ['$scope', 'attachments', function($scope, attachments) {              $scope.gridoptions = {                 // ui-grid code here                 onregisterapi: function( gridapi ) {                     attachments.grid = gridapi;                 }             };         }]     }; }); 

however, there multiple instances of directive

for example, there might primary instance of directive , 1 inside modal, or 1 in sidebar, 1 in modal, etc.

i suppose add property namespaces service...

attachments = {   librarygrid: // ...   somemodalgrid: // ... } 

etc...

i'd prefer avoid making service each possible instance, i.e.:

attachments.factory('somemodalattachments', function() {     return {}; }); 

while work feels inefficient. however, both choices lot better digging modal scope , finding child scopes necessary api.

is there other method haven't considered?

to me depends on usage model.

if you're going have multiple of these , other bits of application going access them, means 1 of few things:

  1. the access initiated grid. have perhaps many list pages, , active list page 1 want deal with. i'd have grid register things wants talk to, , deregister when closes again. other things services (singletons).

  2. there specified number of these grids, , talk them name - want interact list-page-grid, or modal-grid or whatever. have each grid register somewhere central (maybe service else talks to).

  3. the grids subsidiary something. page includes grid directive, , page wants talk grid. pass object directive, have grid register on object. call directive "mygridcommunicationobject = {}", , grid "$scope.mygridcommunicationobject.gridapi = gridapi". doesn't let other bits of application talk grid, if want whatever created grid talk it, works well.

  4. you broadcast. if don't care grid talk to, want talk grid that's visible (say you're resizing them or something) broadcast event them, , grid directives listen event. taking 1 step further, broadcast , include grid id or name in parameter, , have each grid check whether it's me before taking action.

having said options, there's you're doing has bit of code smell it. really, arbitrary bits of application shouldn't want talk directly grid api, should communicating methods on controller holds grid. perhaps examples of want help, feels me model should 1 of grid registering use other services (e.g. resize notifications), or of controller owns grid interacting grid, , other things interacting controller.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -