milo-core

ModelPathMsgAPI

declaration
ModelPathMsgAPI

Subclass of MessengerAPI that is used to translate messages of Messenger on ModelPath to Messenger on Model.

var ModelPathMsgAPI = _.createSubclass(MessengerAPI, 'ModelPathMsgAPI');

module.exports = ModelPathMsgAPI;

ModelPathMsgAPI instance methods

  • init - initializes ModelPathMsgAPI
  • translateToSourceMessage - translates relative access paths of ModelPath to full path of Model
  • createInternalData - changes path in message on model to relative path and adds fullPath property to message data
_.extendProto(ModelPathMsgAPI, {
    init: init,
    translateToSourceMessage: translateToSourceMessage,
    createInternalData: createInternalData,
});

init

function
init()

Option name Type Description
rootPath String

root path of model path

ModelPathMsgAPI instance method
Called by MessengerAPI constructor.

function init(rootPath) {
    MessengerAPI.prototype.init.apply(this, arguments);
    this.rootPath = rootPath;
}

translateToSourceMessage

function
translateToSourceMessage() ->String

Option name Type Description
accessPath String

relative access path to be translated

return String

ModelPathMsgAPI instance method
Translates relative access paths of ModelPath to full path of Model.

function translateToSourceMessage(message) {
    // TODO should prepend RegExes
    // TODO should not prepend changedata too???
    if (message instanceof RegExp)
        return message;
    if (message == 'datachanges')
        return message;
    
    return this.rootPath + message;
}

createInternalData

function
createInternalData() ->Object

Option name Type Description
sourceMessage String

full access path on Model

message String

relative access path on ModelPath

sourceData Object

data received from Model, will be translated as described to be dispatched to ModelPath

return Object

ModelPathMsgAPI instance method
Changes path in message on model to relative path and adds fullPath property to message data.

function createInternalData(sourceMessage, message, sourceData) {
    // TODO return on changedata too???
    if (message == 'datachanges') {
        var internalChanges = sourceData.changes
            .map(truncateChangePath, this)
            .filter(function(change) { return change; });
        var internalData = {
            changes: internalChanges,
            transaction: sourceData.transaction
        };

        return internalData
    }

    var internalData = truncateChangePath.call(this, sourceData);
    return internalData;
}


function truncateChangePath(change) {
    var fullPath = change.path
        , path = _.unPrefix(fullPath, this.rootPath);

    if (typeof path == 'string') {
        var change = _.clone(change);
        change.fullPath = fullPath;
        change.path = path;
        return change;
    }
}