Sha256: 82abf44a4aad74d0f030a58ae8e4bc51d8fb99d85d5831e3cf058ba1ed98b9d0

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

/*
---
 
script: Resource.js
 
description: Make various requests to back end
 
license: Public domain (http://unlicense.org).
 
requires:
  - LSD.Mixin
  - Resource/*
  - More/URI
  
provides: 
  - LSD.Mixin.Resource
 
...
*/

LSD.Mixin.Resource = new Class({
  options: {
    resource: {
      prefix: null,
      name: null
    },
    request: {
      type: 'xhr'
    }
  },
  
  getResource: function(options) {
    if (!options) options = this.options.resource
    if (!this.resource) {
      var name = options.name;
      var prefix = options.prefix;
      if (!name || !prefix) {
        var uri = this.attributes.itemtype.split(/\s+/).getLast();
        if (uri) {
          if (uri.toURI) uri = uri.toURI();
          prefix = uri.get('directory');
          name = uri.get('file');
          /*
            Parses the last URL bit that can be singularized 
          */
          while (!name || !(name = name.singularize())) {
            var dirs = prefix.split('/');
            name = dirs.pop();
            prefix = dirs.join('/')
          }
        }
      }
      var options = Object.clone(this.options.resource);
      if (prefix) options.prefix = prefix;
      options.getRequest = this.bindEvent('getRequest');
      this.resource = new Resource(name, options);
      if (!this.getRequest) this.setAttribute('href', this.resource.getFormattedURL('plural'));
    }
    return this.resource;
  },
  
  getResourceID: function() {
    return this.attributes.itemid;
  },
  
  getModel: function() {
    return this.getResource().init(this.getResourceID() || this.element);
  },
  
  submit: function() {
    var model = this.getModel();
    return model.save.apply(model, arguments);
  },
  
  'delete': function() {
    this.dispose()
    return this.getModel().destroy();
  }
});

LSD.Behavior.define(':resource, [itemscope]', LSD.Mixin.Resource);

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lsd_rails-0.1.2 Packages/lsd/Source/Mixin/Resource.js
lsd_rails-0.1.1 Packages/lsd/Source/Mixin/Resource.js
lsd_rails-0.1 Packages/lsd/Source/Mixin/Resource.js