zoukankan      html  css  js  c++  java
  • 找到MVC框架中前端URL与后端同步的解决方案

    基本思路:

    先用URL标签生成完整的URL字符,前端动态参数的部分以适配符先填充,最后动态参数利用正则匹配进行替换。

    这种方式,可以在各种MVC框架中适用,妙。

    不废话,上码。

    var url = "{url app=xxxxn&act=yyy&id=[0]}";
    url = url.format({$id});
    //String.format 同时匹配[](){}内容方式
    if (!String.prototype.format) {
        String.prototype.format = function () {
            var args = arguments;
            var tag = '';
            return this.replace(/({|(|[)(d+)(}|)|])/g, function (match, m0,m1,m2) {
                tag = m0+m2;
                if(tag=='()' || tag == '{}' || tag == '[]') return typeof args[m1] != 'undefined'? args[m1]: (m0+m1+m2);
            });
        };
    }

    上面用原型的方式改变了String对象的方法,可以适当改写,变成一般的函数传参。

    TP框架中的Url生成函数会对参数进行urlencode,所以括号都会编码掉,要找其它替代符号,如 - 。

  • 相关阅读:
    unittest learning
    C++类和对象
    Linux shell基础(十二)
    Linux shell基础(十一)
    Linux shell基础(十)
    Linux shell基础(九)
    Linux shell基础(八)
    Linux shell基础(六)
    Linux shell基础(七)
    Linux shell基础(五)
  • 原文地址:https://www.cnblogs.com/x3d/p/3909136.html
Copyright © 2011-2022 走看看