Javascript-форум (https://javascript.ru/forum/)
-   Javascript под браузер (https://javascript.ru/forum/css-html/)
-   -   Этот код относиться к ангулар или к чистому js? (https://javascript.ru/forum/css-html/74082-ehtot-kod-otnositsya-k-angular-ili-k-chistomu-js.html)

Shadow 12.06.2018 09:23

Этот код относиться к ангулар или к чистому js?
 
Всем привет
:)
Подскажите пожалуйста этот код можно употреблять в приложении без ангулар или нет? Спасибо:thanks:
DomView.prototype.attach = function (model) {
    this.model = model;
    var that = this; // чтобы this был доступен в функции
    this.model.subscribe(MinesweeperModel.EVENT_UPDATE, function (e) {
        this.redraw(); // перерисовать картинку
    });
};

Rise 13.06.2018 06:34

Можно
function MinesweeperModel() {}
MinesweeperModel.EVENT_UPDATE = 'update';
MinesweeperModel.prototype._handlers = {};
MinesweeperModel.prototype.subscribe = function (type, handler) {
    var handlers = this._handlers;
    if (!handlers[type]) handlers[type] = [];
    handlers[type].push(handler);
};
MinesweeperModel.prototype.unsubscribe = function (type, handler) {
    var handlers = this._handlers[type], i = handlers.indexOf(handler);
    if (i > -1) handlers.splice(i, 1);
};
MinesweeperModel.prototype.dispatch = function (type, data) {
    var handlers = this._handlers[type], i = 0;
    while (i < handlers.length) handlers[i++].call(this, data);
};

function DomView() {}
DomView.prototype.redraw = function () {
    alert('redraw');
};
DomView.prototype.attach = function (model) {
    this.model = model;
    var that = this; // чтобы this был доступен в функции
    this.model.subscribe(MinesweeperModel.EVENT_UPDATE, function (e) {
        that.redraw(); // перерисовать картинку
    });
};

var model = new MinesweeperModel();
var view = new DomView();
view.attach(model);
model.dispatch('update');

Shadow 13.06.2018 18:01

Rise,
а где вы взяли данный код я сейчас действительно делаю сапера и мне поможет любая помощь спасибо

Shadow 13.06.2018 18:10

Rise,
огромное спасибо)


Часовой пояс GMT +3, время: 02:36.