store.js 1.79 KB
'use strict';

const tip = require('plugin/tip');
const socketConf = require('./socket-config');
const conversation = socketConf.conversationMessage;
const slice = Array.prototype.slice;

// EventEmitter
//--------------------------------------------------------
const EventEmitter = function() {};

EventEmitter.prototype = {
    on() {
        var args, ref;

        args = arguments.length >= 1 ? slice.call(arguments, 0) : [];
        return (ref = $(this)).on.apply(ref, args);
    },

    off() {

        var args, ref;

        args = arguments.length >= 1 ? slice.call(arguments, 0) : [];
        return (ref = $(this)).off.apply(ref, args);
    },

    trigger() {
        var args, ref;

        args = arguments.length >= 1 ? slice.call(arguments, 0) : [];
        return (ref = $(this)).triggerHandler.apply(ref, args);
    }
};

EventEmitter.prototype.constructer = EventEmitter;


// api interface
//--------------------------------------------------------
let api = {
    /**
     *  留言
     *  @param {int} conversation 会话id
     *  @param {str} content 内容
     *  @return jqXhr
     */
    leaveMsg: function(content) {
        return $.post('/service/leavemsg/save.json', {
            conversationId: conversation.conversationId,
            content,
        });
    },

    /**
     *  获取订单
     *  TODO:
     *      1. 目前接口不支持分页
     */
    fetchOrders: function() {
        return $.get('/service/order-list');
    }
};




// 消息中心
//--------------------------------------------------------
let bus = new EventEmitter();

bus.on('save.LeaveMSGView', (event, view, data) => {
    api.leaveMsg(...data)
        .then(res => {
            console.log(res);
            view.toggleHide(true);
        });
});

module.exports = {
    EventEmitter,
    api,
    bus
};