Example #1
0
File: util.js Project: hongkou/mit
util['wedgt']['WcEditor'] = function(opts){
    var me = this;
    this.modules = {};
    for(var prop in opts.modules){
        this.modules[prop] = prop;
    };
    this.container = opts.container;
    this.initialed = false;
    riot.observable(this);
    return this;
};
Example #2
0
var Action = function(domain, name){
    this._domain = domain;
    this._name = name;
    this._events = observableFn({});
    this._listeners = {
        execute: [],
        unexecute: [],
        done: [],
        fail: [],
        always: []
    };
};
Example #3
0
var ActionInvocation = function(action, inputs){
    this.id = util.nextId(action.name());
    this.action = action;
    this.inputs = inputs;
    this.outputs = null;
    this.error = null;
    this.executed = false;

    var events = this._events = observableFn({});
    var listeners = this.action._listeners;

    bindNameEventWithContext(this, events, listeners, 'execute');
    bindNameEventWithContext(this, events, listeners, 'unexecute');
    bindNameEventWithContext(this, events, listeners, 'done');
    bindNameEventWithContext(this, events, listeners, 'fail');
    bindNameEventWithContext(this, events, listeners, 'always');
};
Example #4
0
File: nest.js Project: hongkou/mit
var collectize = function(col, options){
    /*
     * make it observable
     */
    riot.observable(col);

    /*
     * add model behaviors
     */
    col._m = util.extend({}, _default_collection);//copy defaults
    col._m = util.extend(col._m, options);//set options

    var _m = col._m;

    col.fetched = function(v){
        return typeof v === 'undefined' ? _m.fetched : _m.fetched = v;
    };

    col.fetch = function(o){
        var _o = _m;
        o && (_o = util.extend({}, _m)) && util.extend(_o, o);

        var filter = _m.filter || {};
        var apiUrl = _o.url;
        var jqxhr = $.ajax({
            type:  _o.method || 'POST',
            url:   apiUrl,
            data:  {filter: filter}
        });

        jqxhr
            .done(function(data, textStatus, jqXHR ){
                if(data.status){
                    col.items = data.result;
                    col.fetched(true);
                    col.trigger('fetch', data.result, o);
                }
                else{
                    col.trigger('fetch-error', data, o);
                }
            })
            .fail(function(jqXHR, textStatus, errorThrown){
                col.trigger('error', errorThrown, o);
            })
            .always(function(){
                //none
            });
        return jqxhr;
    };

    col.append = function(o){
        var _o = _m;
        o && (_o = util.extend({}, _m)) && util.extend(_o, o);

        var filter = _m.filter || {};
        var apiUrl = _o.url;
        var jqxhr = $.ajax({
            type:  _o.method || 'POST',
            url:   apiUrl,
            data:  {filter: filter}
        });
        jqxhr
            .done(function(data, textStatus, jqXHR ){
                if(data.status){
                    col.items = data.result;
                    col.fetched(true);
                    col.trigger('append', data.result, o);
                }
                else{
                    col.trigger('append-error', data, o);
                }
            })
            .fail(function(jqXHR, textStatus, errorThrown){
                col.trigger('error', errorThrown, o);
            })
            .always(function(){
                //none
            });
        return jqxhr;
    };
    return col;
};
Example #5
0
File: nest.js Project: hongkou/mit
var modelize = function(obj, options){
    /*
     * make it observable
     */
    riot.observable(obj);

    /*
     * add model behaviors
     */
    obj._m = util.extend({}, _default_model);//copy defaults
    obj._m = util.extend(obj._m, options);//set options

    var _m = obj._m;
    obj.fetched = function(v){
        return typeof v === 'undefined' ? _m.fetched : _m.fetched = v;
    };

    obj.has = function(attr) {
        return this.get(attr) != null;
    },
    obj.isNew = function() {
        return !this.has(_m.idAttribute);
    };
    obj.toObject = function() {
        var obj = {};
        for(var n in this){
            if(typeof this[n] != 'function'){
                obj[n] = this[n];
            }
        }
        return obj;
    };
    obj.fetch = function(id, o){
        if(typeof o == 'undefined'){
            if(typeof id == 'object'){
                o = id;
                id = null;
            }
        }

        var _o = _m;
        o && (_o = util.extend({}, _m)) && util.extend(_o, o);
        id || (id = this[_m.idAttribute]);

        if(!id){
            console.error('fail to fetch: no id');
            return false;
        }

        var apiUrl = _o.url + '_' +id;
        var jqxhr = $.ajax({
            type:  _o.method || 'GET',
            url:   apiUrl
        });

        jqxhr
            .done(function(data, textStatus, jqXHR ){
                if(data.status){
                    obj.trigger('fetch', data.result, o);
                }
                else{
                    obj.trigger('fetch-error', data, o);
                }
            })
            .fail(function(jqXHR, textStatus, errorThrown){
                obj.trigger('error', errorThrown, o);
            })
            .always(function(){
                //none
            });
        return jqxhr;
    };

    obj.append = function(id, o){
        if(typeof o == 'undefined'){
            if(typeof id == 'object'){
                o = id;
                id = null;
            }
        }

        var _o = _m;
        o && (_o = util.extend({}, _m)) && util.extend(_o, o);
        id || (id = this[_m.idAttribute]);

        if(!id){
            console.error('fail to append: no id');
            return false;
        }

        var apiUrl = _o.url + '_' +id;
        var jqxhr = $.ajax({
            type:  _o.method || 'GET',
            url:   apiUrl
        });

        jqxhr
            .done(function(data, textStatus, jqXHR ){
                if(data.status){
                    obj.trigger('append', data.result, o);
                }
                else{
                    obj.trigger('append-error', data, o);
                }
            })
            .fail(function(jqXHR, textStatus, errorThrown){
                obj.trigger('error', errorThrown, o);
            })
            .always(function(){
                //none
            });
        return jqxhr;
    };

    var getQueryString = function(query){
        var params = [];
        for(var name in query){
            params.push(name + '=' + query[name]);
        }
        return params.join('&');
    };
    obj.save = function(attributes, o){
        var _o = _m;
        o && (_o = util.extend({}, _m)) && util.extend(_o, o);
        var method = this.isNew() ? 'POST' : 'PUT';
        var apiUrl = this.isNew() ? _o.url : _o.url + "_" + this[_m.idAttribute];
        var data = this.toObject();
        var queryString = getQueryString(data.query||{});
        queryString!='' && (apiUrl+='?'+queryString);
        var jqxhr = $.ajax({
            type: method,
            url:   apiUrl,
            data: data
        });

        jqxhr
            .done(function(data, textStatus, jqXHR ){
                if(data.status){
                    obj.fetched(true);
                    obj.trigger('save', data.result, o);
                }
                else{
                    obj.trigger('save-error', data, o);
                }
            })
            .fail(function(jqXHR, textStatus, errorThrown ){
                obj.trigger('error', errorThrown, o);
            })
            .always(function(){
                //none
            });
        return jqxhr;
    };

    obj.get = function(name){
        return this[name];
    };

    obj.set = function(name, value, o){
        if(name == 'myobj' && typeof value=="object"){
            util.extend(this,value);
        }
        else if(name == 'randomNotePic' && typeof value=="object"){
            util.extend(this,value);
        }
        else if(value=="object"){
            this[name] = value;
        }
        else{
            var oldValue = this[name];
            this[name] = value;
            if(o && o.silent){

            }
            else{
                this.trigger('change:' + name, this, value, oldValue);
                this.trigger('change', this);
            }
        }
    };
    return obj;
};
Example #6
0
File: ws.js Project: hongkou/vmark
"use strict";
var SockJS = require('sockjs');
var riot = require('seedriot');
var base_Url = '/bot';
var orgId = __page.user.posts[0].org;
var default_Url = '/bot?r=' + orgId;

function WebSocket(client){
    this.prefix = base_Url;
    this.ws = client;
    this.ready = false;
    this.receiveCache = [];
    this.sendCache = [];
}
WebSocket.prototype = Object.create(riot.observable({}));
WebSocket.prototype.send = function(data){
    if(!this.ready){
        this.sendCache.push(data);
        return;
    }
    this.ws.send(JSON.stringify(data));
};
WebSocket.prototype.onOpen = function(cb){
    var me = this;
    me.ws.onopen = function(){
        me.ready = true;
        me.receiveCache.forEach(function(msg){
            me._handleMsg(msg);
        });
        me.sendCache.forEach(function(msg){
            me._handleSend(msg);