Use listening (addEventListener) and AngularJs in angularjs

Source: Internet
Author: User
Tags hasownproperty

Use listening (addEventListener) and AngularJs in angularjs

Usage scope: subclass. js to pass values to the parent class. js or other pages. js


Let alone code first

1. Events. js

angular.module('pr.services')  .provider('Notifications',    function() {      //https://github.com/trochette/Angular-Design-Patterns-Best-Practices/blob/master/js/base/EventDispatcher.js      var eventDispatcher = {        _listeners: {},        _cache: {},        event: {          // account: object          ACCOUNT_CHANGE: 'PR_ACCOUNT_CHANGE',          //Risk Measures          RISK_MEASURE_CHANGE: 'PR_RISK_MEASURE_CHANGE',        },        addEventListener: function(type, listener, cache) {          if (!this._listeners[type]) {            this._listeners[type] = [];          }          this._listeners[type].push(listener);          if (cache && this._cache.hasOwnProperty(type)) {            listener.apply(null, this._cache[type]);          }        },        removeEventListener: function(type, listener) {          if (this._listeners[type]) {            var index = this._listeners[type].indexOf(listener);            if (index !== -1) {              this._listeners[type].splice(index, 1);            }          }        },        dispatchEvent: function() {          var listeners;          var data = arguments;          var type = arguments[0];          if (typeof type !== 'string') {            console.warn('EventDispatcher', 'First params must be an event type (String)');          } else {            // console.log(data);            listeners = this._listeners[type];            this._cache[type] = data;            if (listeners) {              listeners.forEach(function(listener) {                listener.apply(null, data);              });            }          }        },        getLastCalledValue: function(type) {          return this._cache.hasOwnProperty(type) ? this._cache[type] : [];        }      };      eventDispatcher.notify = eventDispatcher.dispatchEvent;      this.$get = function() {        return eventDispatcher;      };    });


Introduce the Code:

A. eventDispatcher. Policy Function ----------- notify riskMeasureHandler function when data changes.

Usage:

<span style="white-space:pre"></span>Notifications.notify(Notifications.event.RISK_MEASURE_CHANGE,$scope.riskMeasureAll);


B. removeEventListener function -------------- remove listener when scope is removed

Usage:

$scope.$on('$destroy', function() {   Notifications.removeEventListener(Notifications.event.RISK_MEASURE_CHANGE, riskMeasureHandler);});
C. addEventListener function ----------------- adds a listener. If listen y changes its value, it is changed. Every time notify changes, it changes immediately.

Usage:

var riskMeasureAllAfter;var riskMeasureHandler = function(ev, riskMeasureAll) {  riskMeasureAllAfter = riskMeasureAll;};Notifications.addEventListener(Notifications.event.RISK_MEASURE_CHANGE, riskMeasureHandler, true);$scope.$on('$destroy', function() {  Notifications.removeEventListener(Notifications.event.RISK_MEASURE_CHANGE, riskMeasureHandler);});

D. RISK_MEASURE_CHANGE -------- name of the event to be monitored, which uniquely identifies each event. (Defined in eventDispatcher's event)

Definition:

RISK_MEASURE_CHANGE: 'PR_RISK_MEASURE_CHANGE',
Usage:
Notifications.notify(Notifications.event.RISK_MEASURE_CHANGE,$scope.riskMeasureAll);


Note:

When the value is returned: the object should not only have the same value, but also be the same object. Use _. find to find the object.

        $scope.riskMeasureBefore = _.find($scope.riskMeasures, {          name: st.getRiskMeasure()        });        if(!riskMeasureAllAfter){          $scope.riskMeasure = $scope.riskMeasureBefore;        }        else{          //have same data ,but not the same object          $scope.riskMeasure = _.find($scope.riskMeasures, {            name: riskMeasureAllAfter.changed.name          });        }






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.