Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide propagation mechanism for completion. #2314

Closed
oliverfoster opened this issue Jan 7, 2019 · 0 comments
Closed

Provide propagation mechanism for completion. #2314

oliverfoster opened this issue Jan 7, 2019 · 0 comments
Assignees

Comments

@oliverfoster
Copy link
Member

oliverfoster commented Jan 7, 2019

Subject of the issue

Cannot easily listen to completion changes without listening to all collections or models.

Expected behaviour

There should be a mechanism to bubble events from component to course.

Proposal:

// core/js/modelEvent.js
define(function() {

  var ModelEvent = Backbone.Controller.extend({

    type: null,
    target: null,
    value: null,
    bubbles: true,
    deepPath: null,
    target: null,
    timeStamp: null,

    initialize: function(type, target, value) {
      this.type = type;
      this.target = target;
      this.value = value;
      this.deepPath = [target];
    },

    stopPropagation: function() {
      this.bubbles = false;
    },

    addPath: function(target) {
      this.deepPath.unshift(target);
    }

  });

  return ModelEvent;

});

// core/js/model/adaptModel.js
define([
    'core/js/adapt',
    'core/js/modelEvent',
], function (Adapt, ModelEvent) {

    var AdaptModel = Backbone.Model.extend({

        bubblingEvents: [
            'change:_isComplete',
            'change:_isInteractionComplete'
        ],

        setupChildListeners: function() {
            this.listenTo(children, {
                "all": this.onAll,
                "bubble": this.bubble
            });
        }

        onAll: function(type, model, value) {
            if (!_.contains(this.bubblingEvents, type)) return;
            var event = new ModelEvent(type, model, value);
            this.bubble(event);
        },

        bubble: function(event) {
            if (!event.bubbles) return;
            event.addPath(this);
            this.trigger("bubble:"+event.type, event);
            this.trigger("bubble", event);
        }

    });

    return AdaptModel;

});


// example
Adapt.course.on("bubble:change:_isComplete", function(event) {
  /* 
   * event.type = "change:_isComplete";
   * event.stopPropagation(); // stop event from bubbling
   * event.bubbles = true/false; // has stopPropagation been called?
   * event.deepPath = [grandParent, parent, target]; // path of bubble
   * event.target; // instance on which event occurred
   * event.value; // value of event
   *
   */
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant