So I am writing an Angular Frontend for a pre-existing Rails Application. I understand that using $q is at best an intermediate step before converting the back end to serve a REST Api directly to ngResource through JSON, but unfortunately for the time being there is too much logic residing in various Ruby locations to easily rewrite everything into a purely Angular format.
The question is, how do I correctly instantiate and handle ng attributes in the returned partials. The below is a sanitized version of the code.
Company.Controller.TypeAccordionController = (scope, http, element, q, typeJQueryService) ->
scope.getTheDamnType = (id) ->
typeJQueryService.multipart(id).then (response)->
angular.element('.datePicker').datepicker()
Company.Controller.TypeAccordionController.$inject = ['$scope', '$http', '$element', '$q','typeJQuery']
The returned HAML is being correctly displayed on the page and the datepicker is being instantiated successfully. The returned HAML contains the following:
= ff.check_box :completed, :class => "field sale_#{sale.price}", :disabled => sale.completed?
Through the following service:
Company.MyModule.factory 'TypeJQuery', ($q, $rootScope) ->
multipart: (element) ->
deferred = $q.defer()
$.get "..." + element, (data) ->
$rootScope.$apply ->
deferred.resolve data
deferred.promise
With the following Javascript being defined in a directive:
Company.Directive.DatePickerDirective = ->
restrict: 'C'
controller: Company.Controller.TypeAccordionController
link: (scope, element, attrs) ->
$(".type-form input[type='checkbox']").click (e)->
$checkbox = $(e.target)
$date = $checkbox.parent('.checkbox').siblings(".target").find('.actually_completed_on_date')
if $checkbox.is(':checked')
if $date.val() == undefined || $date.val().length == 0
$date.val($.datepicker.formatDate("dd-M-yy", new Date()))
else
$date.val("")
Company.MyModule.directive 'datePicker', Company.Directive.DatePickerDirective
Please note that before wrapping Angular around this, the javascript used to live directly in the asset pipeline, and used to run just fine.