Lua pureMVC

分享一个lua语言版本的pureMVC。框架

这个是一个根据AS3(ActionScript 3) pureMVC而转换过来的lua pureMVC。全部的接口彻底跟AS3版本一致,原本是想用在项目之中的,惋惜并未被实际应用,虽然,测试了是可用的,因此在此开发,但愿有人须要的能够使用,如果有什么错误也很是但愿获得你的回复。函数

如果想使用,能够直接查看网上的pureMVC 文档,我并未对任何一个函数更名或者更换参数位置。测试

注意,这个PureMVC中的 class(ClassName, BaseName) 函数并不提供,由于此框架本意就是为了用于cocos2d-x-lua中。ui

为方便用户在此提供函数:lua

function class(classname, ...)
    local cls = {__cname = classname}

    local supers = {...}
    for _, super in ipairs(supers) do
        local superType = type(super)
        assert(superType == "nil" or superType == "table" or superType == "function",
            string.format("class() - create class \"%s\" with invalid super class type \"%s\"",
                classname, superType))

        if superType == "function" then
            assert(cls.__create == nil,
                string.format("class() - create class \"%s\" with more than one creating function",
                    classname));
            -- if super is function, set it to __create
            cls.__create = super
        elseif superType == "table" then
            if super[".isclass"] then
                -- super is native class
                assert(cls.__create == nil,
                    string.format("class() - create class \"%s\" with more than one creating function or native class",
                        classname));
                cls.__create = function() return super:create() end
            else
                -- super is pure lua class
                cls.__supers = cls.__supers or {}
                cls.__supers[#cls.__supers + 1] = super
                if not cls.super then
                    -- set first super pure lua class as class.super
                    cls.super = super
                end
            end
        else
            error(string.format("class() - create class \"%s\" with invalid super type",
                        classname), 0)
        end
    end

    cls.__index = cls
    if not cls.__supers or #cls.__supers == 1 then
        setmetatable(cls, {__index = cls.super})
    else
        setmetatable(cls, {__index = function(_, key)
            local supers = cls.__supers
            for i = 1, #supers do
                local super = supers[i]
                if super[key] then return super[key] end
            end
        end})
    end

    if not cls.ctor then
        -- add default constructor
        cls.ctor = function() end
    end
    cls.new = function(...)
        local instance
        if cls.__create then
            instance = cls.__create(...)
        else
            instance = {}
        end
        setmetatableindex(instance, cls)
        instance.class = cls
        instance:ctor(...)
        return instance
    end
    cls.create = function(_, ...)
        return cls.new(...)
    end

    return cls
end

函数来源cocos2d-x-Lua。spa

 下载地址:http://files.cnblogs.com/files/cqf-zuifangxing/pureMVC.rarcode

相关文章
相关标签/搜索