Sha256: a9ab499b920b51094440c57cf22413b0e93487caf1bdd0905cd1bd6f42e9eeca

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module AngularSprinkles
  module JavaScript
    PREFIX = :sprinkles
    APP_NAME = "#{PREFIX}App"
    CONTROLLER_NAME = "#{PREFIX}Ctrl"
    CONTROLLER_FN = "window.#{CONTROLLER_NAME}Fn"
    SERVICE_QUEUE = "#{CONTROLLER_FN}.serviceQueue"

    PLUGIN = <<-BlockOfJavaScript
    (function (window, document, angular) {
      window.sprinkles = angular.module('#{APP_NAME}', []);
      window.onload = function () {
        var queue,
          app = angular.module('#{APP_NAME}'),
          doc = document.documentElement,
          ctrlName = 'Ctrl';
        #{CONTROLLER_FN} = #{CONTROLLER_FN} || function(){};
        app.controller(ctrlName, #{CONTROLLER_FN});
        doc.setAttribute('data-ng-controller', ctrlName + ' as #{CONTROLLER_NAME}');
        angular.bootstrap(doc, [app.name]);
      };
    }(window, document, window.angular));
    BlockOfJavaScript

    ConstructorWrapper = ->(string) do
      <<-BlockOfJavaScript
      #{CONTROLLER_FN} = #{CONTROLLER_FN} || function ($injector) {
        #{string}

        #{SERVICE_QUEUE}.forEach(function (service) {
          this[service] = $injector.get(service);
        }.bind(this));
        #{SERVICE_QUEUE} = [];
      };
      #{SERVICE_QUEUE} = [];
      BlockOfJavaScript
    end

    ConstructorThis = ->(object, key) do
      "this.#{key} = #{object.to_json};"
    end

    RegisterService = ->(method) do
      "#{SERVICE_QUEUE}.push('#{method}')"
    end

    BindService = ->(method, input) do
      "#{CONTROLLER_NAME}.#{method}(#{input.join(',')})"
    end

    BindVariable = ->(key, attribute) do
      [CONTROLLER_NAME, key, attribute].flatten.compact.join('.')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
angular_sprinkles-0.0.8 lib/angular_sprinkles/java_script.rb
angular_sprinkles-0.0.7 lib/angular_sprinkles/java_script.rb
angular_sprinkles-0.0.6 lib/angular_sprinkles/java_script.rb