Sha256: d3f59292097dd7dc0523dfa14603b142924d74310275a994d5ba61ed9f7494f6

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

import jQuery.*;

typedef AsyncCallback = Dynamic -> Void

class RailsViewComponent {
  public static var com_name : String;
  
  public static function start(name, ?pos:haxe.PosInfos){
    com_name = name;
    var klass = Type.resolveClass(pos.className);
    var class_name = StringTools.replace(name, "/", "-");
    var coms = new JQuery(".rails_view_component-"+class_name);
    for(com in coms){
      var id = new JQuery(com).attr("id");
      var c = Type.createInstance(klass, [id]);
      c.onLoad();
    }
  }
 
  public function onLoad(){}

  private var id : String;
  private var dom : JQuery;
  private var callbacks = new Map<String, Array<AsyncCallback>>();

  public function new(id : String){
    this.id = id;
    this.dom = new JQuery("#"+id);
  }

  public function getId(){ return this.id;}
  public function getDOM(){ return this.dom; }

  public function addCallback(route:String, func:AsyncCallback){
    if(!this.callbacks.exists(route)){
      this.callbacks.set(route, new Array<AsyncCallback>());
    }
    this.callbacks.get(route).push(func);
  }
  private function callCallback(route:String, data:Dynamic){
    if(this.callbacks.exists(route)){
      for(func in this.callbacks.get(route)){
        func(data);
      }
    }
  }

  public function sendAsync(route:String, params:Dynamic){
    var xs = route.split("@");
    var action = xs[0];
    var method = xs[1];
    var url = "/rails_view_component/"+ com_name +"/"+ action;
    var callback = function(data,status,xhr){
      callCallback(route, data);
    }
    var query = switch(method){
    case "get":
      JQueryStatic.get(url, params, callback);
    case "post", _:
      JQueryStatic.post(url, params, callback);
    }
    query.fail(function(e){
      trace("ERROR:"+e);
    });
  } 
}



Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_view_component-1.0.0 lib/generators/rails_view_component/templates/RailsViewComponent.hx