ES2015 单例或服务提供商或模块,用于敲除.js组件

ES2015 Singleton or Service Provider or Module for Knockout.js components

本文关键字:js 用于 组件 模块 单例 服务 提供商 ES2015      更新时间:2023-09-26

我有大量的淘汰组件,目前正在使用KO.postbox进行通信。

我现在想创建一个中央服务提供商/存储库/单例,它集中存储所有这些组件的数据并提供初始化、API 和其他功能。

该页面只有一个窗口/会话,但每个窗口/会话都有 3-7 个 Knockout 组件,在某些情况下,同一组件会在页面上多次加载。 结果是通过 API 加载数据的组件和也需要相同数据的组件之间的大量颤动。

我目前的方法是使用单例模式(其他方法很乐意考虑(。 唯一不可更改的要求是:

  1. 从一个中央"存储库"中存储和检索多个KO组件的数据
  2. 写在ES2015中,能够与Babel一起工作
  3. 可作为模块加载和导出

当前代码的问题是

一个。这被设置为未定义 babel,例如 this.instance = null引发无法设置未定义的实例的错误。b.我不确定这是最好的方法,或者我是否可以使其工作

代码如下

const ko = require('knockout')
    , moment = require('moment')
    , postbox = require('knockout-postbox')
    , aja = require('aja');

const myServiceSingleton = () =>{ 
    this.instance = null;
    this.array1 = ko.observable([]);
    this.array2 = ko.observable([]);
    // revealing module pattern that handles initialization of our new singleton service provider
    const initializeNewModule = () => {
        const setArray1 = (array) => {
            console.info( 'Set Array1 Called' );
            this.array1(array);
        };
        const getArray1 = () => {
            console.info( 'Get Array1 Called' );
            return this.array1();
        };
        const setArray2 = (array) => {
            console.info( 'Set Array2 Called' );
            this.array2(array);
        };
        const getArray2 = () => {
            console.info( 'Get Array2 Called' );
            return this.array2();
        };
        const myAwesomeFunction = () => {
            // Perform some amazing computations on Array1 and Array 2
        };
        return {
            setArray1 : setArray1,
            getArray1 : getArray1,
            setArray2 : setArray2,
            getArray2 : getArray2,
            myAwesomeFunction : myAwesomeFunction
        };
    };
    // handles the prevention of additional instantiations
    const getInstance = () => {
        if( ! this.instance ) {
            this.instance = new initializeNewModule();
        }
        return this.instance;
    };
    return {
        getInstance : getInstance
    };
};
module.exports = myServiceSingleton;

---------编辑----------

希望这对其他人有所帮助...

const ko = require('knockout')
    , moment = require('moment')
    , postbox = require('knockout-postbox')
    , aja = require('aja');
const array1 = ko.observable([]);
const array2 = ko.observable([]);
const secretFlag = ko.observable(false);
const myAmazingSingleton = {
    setArray1(newArray) {
        console.info( newArray);
        array1(newArray);
    },
    getArray1() {
        console.info( 'Get Array1 Called' );
        return array1();
    },
    getArray2() {
        return array2();
    },
    setArray2(newArray) {
        console.info('Set Array2 Called');
        array2(newArray);
    },
    getArray1Observable() {
        return array1 ;
    },
    myAwesomeFunction() {
        // Perform some amazing computations on Array1 and Array 2
        array1.map //etc etc
    }
};
export default myAmazingSingleton ;

使用非常简单:

import ArrayFunctions from 'myAmazingSingleton';
let array1 = ArrayFunctions.getArray1();

数据可在多个挖空组件中使用

不能将箭头函数用作构造函数。你真的应该只使用一个简单的对象文字:

const myServiceSingleton = {
    array1: ko.observable([]),
    array2: ko.observable([]),
    setArray1(array) {
        console.info( 'Set Array1 Called' );
        this.array1(array);
    },
    getArray1() {
        console.info( 'Get Array1 Called' );
        return this.array1();
    },
    setArray2(array) {
        console.info( 'Set Array2 Called' );
        this.array2(array);
    },
    getArray2() {
        console.info( 'Get Array2 Called' );
        return this.array2();
    },
    myAwesomeFunction() {
        // Perform some amazing computations on Array1 and Array 2
    }
};

如果你坚持,你可以做一个

export function getInstance() {
    return myServiceSingleton;
}

甚至懒惰地初始化它,但通常你应该export default它。

我认为您要使用的单例是主视图模型,我的意思是设置Knockout的常用方法。使用组件的params从主视图模型传递组件需要共享的任何可观察量。