Sha256: d8608559723780ba076f42e417eb20b419526adda743a9dd6c212836e26d2d1c

Contents?: true

Size: 1.84 KB

Versions: 13

Compression:

Stored size: 1.84 KB

Contents

(function () {
  var Routes = function () {
    var self = this,
        routes = <%= @routes.to_json %>;

    self.defaultParams = {}

    var serialize = function(obj, prefix) {
      var str = [];
      for(var p in obj) {
        if (obj.hasOwnProperty(p)) {
          var k = prefix ? prefix + "[" + (Array.isArray(obj) ? '' : p) + "]" : p, v = obj[p];
          str.push(typeof v == "object" ?
            serialize(v, k) :
            encodeURIComponent(k) + "=" + encodeURIComponent(v));
        }
      }
      return str.join("&");
    }

    var omit = function (hash, key) {
      var hash = angular.copy(hash);
      delete hash[key]
      return hash
    }


    angular.forEach(routes, function (val, key) {
      var result = '';

      var gsub = function(params) {
        if (params.format) {
          result += '.' + params.format
        }

        var params = omit(params, 'format');
        angular.forEach(params, function (v, k) {
          var subst = ':' + k;
          if (result.search(subst) != -1) {
            result = result.replace(subst, v);
            params = omit(params, k);
          }
        })

        if (result.search(/\*\w+/)!=-1) {
          result = result.replace(/\*(\w+)/, function (a, b) {
            return params[b];
          })
        }
        
        if (Object.keys(params).length)
          result += '?'+serialize(params)

        return result;
      }

      self[key+'_path'] = function (params) {
        var params = angular.extend(angular.copy(val.defaults), params || {});
        result = val.path;
        var defaultParams = angular.copy(self.defaultParams);
        return gsub(angular.extend(defaultParams, params));
      }

      self[key+'_url'] = function (params) {
        return window.location.origin + self[key+'_path'](params)
      }
    })
  }

  window.Routes = new Routes();

}).call(this)

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
oxymoron-1.2.8 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.2.7 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.2.6 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.2.5 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.2.4 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.2.3 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.2.2 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.2.1 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.2.0 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.1.9 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.1.8 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.1.7 app/assets/javascripts/oxymoron/routes.js.erb
oxymoron-1.1.6 app/assets/javascripts/oxymoron/routes.js.erb