How to use the esp-js.model function in esp-js

To help you get started, we’ve selected a few esp-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github esp / esp-js / examples / esp-chat-react / js / model / eventProcessors / MessageSubscription.js View on Github external
var esp = require("esp-js");
var modelRouter = require('../modelRouter');

// !!! Please Note !!!
// We are using localStorage as an example, but in a real-world scenario, this
// would involve XMLHttpRequest, or perhaps a newer client-server protocol.
// The function signatures below might be similar to what you would build, but
// the contents of the functions are just trying to simulate client-server
// communication and server-side processing.

var MessageSubscription = function () {
    esp.model.DisposableBase.call(this);
};

MessageSubscription.prototype = Object.create(esp.model.DisposableBase.prototype);

MessageSubscription.prototype.start = function () {
    // simulate retrieving data from a database
    var rawMessages = JSON.parse(localStorage.getItem("messages"));

    // simulate success callback
    setTimeout(function () {
        modelRouter.publishEvent("messagesReceived", {rawMessages: rawMessages});
    }, 0);
};

module.exports = MessageSubscription;
github esp / esp-js / examples / esp-chat-react / js / model / eventProcessors / SendMessageWorkItem.js View on Github external
var esp = require("esp-js");
var uuid = require('node-uuid');
var modelRouter = require('../modelRouter');

// !!! Please Note !!!
// We are using localStorage as an example, but in a real-world scenario, this
// would involve XMLHttpRequest, or perhaps a newer client-server protocol.
// The function signatures below might be similar to what you would build, but
// the contents of the functions are just trying to simulate client-server
// communication and server-side processing.

var SendMessageWorkItem = function () {
    esp.model.DisposableBase.call(this);
};

SendMessageWorkItem.prototype = Object.create(esp.model.DisposableBase.prototype);

SendMessageWorkItem.prototype.send = function (text, threadId, threadName) {
    // simulate writing to a database
    var rawMessages = JSON.parse(localStorage.getItem("messages"));
    var rawMessage = {
        id: uuid.v4(),
        threadId: threadId,
        threadName: threadName,
        authorName: "Bill", // hard coded for the example
        text: text,
        timestamp: Date.now()
    };
    rawMessages.push(rawMessage);
    localStorage.setItem("messages", JSON.stringify(rawMessages));

    // simulate success callback
github esp / esp-js / examples / smallEndToEnd / model / eventProcessors / NotionalEventProcessor.js View on Github external
* you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 // notice_end

import esp from 'esp-js';

class NotionalEventProcessor extends esp.model.DisposableBase {
    constructor(router) {
        super();
        this._router = router;
    }
    start() {
        this._observeInitEvent();
        this._observeUserChangedNotionalEvent();
    }
    _observeInitEvent() {
        this.addDisposable(this._router
            .getEventObservable("modelId1", 'initEvent')
            .observe((model, event) => {
                // set the default notional
                model.notional.value = event.notional;
            })
        );
github esp / esp-js / examples / smallEndToEnd / controllers / MainController.js View on Github external
* you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 // notice_end

import esp from 'esp-js';

class MainController extends esp.model.DisposableBase {
    constructor(router) {
        super();
        this._router = router;
    }
    start() {
        this._syncWithModel();

        // simulate a user changing something, anything that wants to change the model does so via an event,
        // perhaps a controller sends one in response to a user action (if used in the GUI), or a service receiving requests off the
        // network publishes one (if used on the server)
        setTimeout(() => {
           this._router.publishEvent("modelId1", "userChangedNotionalEvent", { notional: 1000000 });
        }, 6000);
    }
    _syncWithModel() {
        this.addDisposable(
github esp / esp-js / examples / multiModel / app.js View on Github external
console.log("Model update received. Total car cost: %s", model.totalCarCost);

            })
        );
    }
    addCar(type) {
        console.log("Adding %s ----------", type);
        this._router.publishEvent(this._productionLineId, "addCar", { type: type });
    }
    removedCar(carId) {
        console.log("Removing %s ----------", type);
        router.publishEvent(this._productionLineId, "removeCar", { carModelId: carId });
    }
}

class CarController extends esp.model.DisposableBase  {
    constructor(carId, router) {
        super();
        this._carId = carId;
        this._router = router;
    }
    start() {
        this.addDisposable(
            router.getModelObservable(this._carId).observe(model => {
                console.log("Car %s model update received.", this._carId);
            })
        );
    }
    upgradEngine() {
        console.log("Upgrading engine on car %s ----------", this._carId);
        router.publishEvent(this._carId, "modifySpecsEvent", { hasEngineUpgrade: true });
    }
github esp / esp-js / examples / esp-chat-react-es6 / js / model / eventProcessors / ThreadSectionEventProcessor.js View on Github external
var ThreadSectionEventProcessor = function () {
    esp.model.DisposableBase.call(this);
};
github esp / esp-js / examples / esp-chat-react / js / model / eventProcessors / ChatAppEventProcessor.js View on Github external
"use strict";

var esp = require('esp-js');
var MessageSubscription = require('./MessageSubscription');
var modelRouter = require('./../modelRouter');

var ChatAppEventProcessor = function () {
    esp.model.DisposableBase.call(this);
    this.messageSubscription = undefined;
};

ChatAppEventProcessor.prototype = Object.create(esp.model.DisposableBase.prototype);

ChatAppEventProcessor.prototype.start = function () {
    this.messageSubscription = new MessageSubscription();
    this.addDisposable(this.messageSubscription);
    this.observeInitEvent();
    this.observeThreadSelected();
    this.observeMessagesReceived();
};

ChatAppEventProcessor.prototype.observeInitEvent = function () {
    this.addDisposable(modelRouter
        .getEventObservable("initEvent")
        .observe(function () {
            this.messageSubscription.start();
        }.bind(this))
        );
github esp / esp-js / examples / esp-chat-react / js / model / eventProcessors / MessageSectionEventProcessor.js View on Github external
"use strict";

var esp = require('esp-js');
var entities = require('../entities');
var SendMessageWorkItem = require('./SendMessageWorkItem');
var modelRouter = require('../modelRouter');

var MessageSectionEventProcessor = function () {
    esp.model.DisposableBase.call(this);
    this.messageSubscription = undefined;
};

MessageSectionEventProcessor.prototype = Object.create(esp.model.DisposableBase.prototype);

MessageSectionEventProcessor.prototype.start = function () {
    this.observeThreadSelected();
    this.observeMessagesReceived();
    this.observeMessageSent();
};

MessageSectionEventProcessor.prototype.observeMessagesReceived = function () {
    this.addDisposable(modelRouter
        .getEventObservable("messagesReceived", esp.EventStage.commited)
        .observe(function (model) {
            this._updateMessages(model);
            model.messageSection.hasChanges = true;
        }.bind(this))
        );
};
github esp / esp-js / examples / esp-chat-react / js / model / eventProcessors / ThreadSectionEventProcessor.js View on Github external
"use strict";

var esp = require('esp-js');
var entities = require('../entities');
var modelRouter = require('../modelRouter');

var ThreadSectionEventProcessor = function () {
    esp.model.DisposableBase.call(this);
};

ThreadSectionEventProcessor.prototype = Object.create(esp.model.DisposableBase.prototype);

ThreadSectionEventProcessor.prototype.start = function () {
    this.observeThreadSelected();
    this.observeMessagesReceived();
};

ThreadSectionEventProcessor.prototype.observeMessagesReceived = function () {
    this.addDisposable(modelRouter
        .getEventObservable("messagesReceived", esp.EventStage.commited)
        .observe(function (model, event) {
            for (var i = 0; i < event.rawMessages.length; i++) {
                var rawMessage = event.rawMessages[i];
                var thread = model.threadSection.threadsById[rawMessage.threadId];
                var messageTime = new Date(rawMessage.timestamp);
                if (thread === undefined) {
                    thread = new entities.Thread(
github esp / esp-js / examples / esp-chat-react-es6 / js / model / eventProcessors / MessageSectionEventProcessor.js View on Github external
"use strict";

var esp = require('esp-js');
var entities = require('../entities');
var SendMessageWorkItem = require('./SendMessageWorkItem');
var modelRouter = require('../modelRouter');

var MessageSectionEventProcessor = function () {
    esp.model.DisposableBase.call(this);
    this.messageSubscription = undefined;
};

MessageSectionEventProcessor.prototype = Object.create(esp.model.DisposableBase.prototype);

MessageSectionEventProcessor.prototype.start = function () {
    this.observeThreadSelected();
    this.observeMessagesReceived();
    this.observeMessageSent();
};

MessageSectionEventProcessor.prototype.observeMessagesReceived = function () {
    this.addDisposable(modelRouter
        .getEventObservable("messagesReceived", esp.ObservationStage.commited)
        .observe(function (model) {
            this._updateMessages(model);
            model.messageSection.hasChanges = true;
        }.bind(this))
        );
};