ai / evil-blocks

Tiny framework for web pages to split your app to separated blocks
MIT License
131 stars 10 forks source link

Shortcut doesn't work #5

Closed onemanstartup closed 10 years ago

onemanstartup commented 11 years ago

https://github.com/ai/evil-blocks/blob/master/lib/evil-blocks.js#L195 This is not working.

ai commented 10 years ago

Do you use CoffeeScript?

If init shortcut return object, evil.block will call every method of this object. It was used in old API to split logic:

evil.block '.gallery-control', ->

  initSlideshow: ->
    # do some code

  initControls: ->
    # do some code

I know, that is bad logic, but unfortunately, I have a lot of legacy code and can’t change this shortcut API in current releases :(.

ai commented 10 years ago

Sorry, for some delay. I return from vacation only today :).

ai commented 10 years ago

BTW, can you show your code example? Maybe problem is in another place.

onemanstartup commented 10 years ago

Hi. Yeah I know :) Привет) Don't work

evil.block '.ajax-autocomplete', ->
  $(@input_autocomplete).autocomplete
    source: (request, response) =>
      dependent_params = ''
      $(@dependent).each (index, element) ->
        dependent_params += '&dependent' + $(element).serialize().replace(/.+%5B/, '%5B')
      $.ajax
        url: $(@input_autocomplete).data('url')
        data: '&start_with=' + $(@input_autocomplete).val() + dependent_params
        dataType: "json"
        success: (data) ->
          response data
    minLength: 2
    appendTo: $(@input_autocomplete).parent()
    select: (event, ui) =>
      $(@result_autocomplete).val(ui.item.id)

image

works

evil.block '.ajax-autocomplete',
  init: ->
    $(@input_autocomplete).autocomplete
      source: (request, response) =>
        dependent_params = ''
        $(@dependent).each (index, element) ->
          dependent_params += '&dependent' + $(element).serialize().replace(/.+%5B/, '%5B')
        $.ajax
          url: $(@input_autocomplete).data('url')
          data: '&start_with=' + $(@input_autocomplete).val() + dependent_params
          dataType: "json"
          success: (data) ->
            response data
      minLength: 2
      appendTo: $(@input_autocomplete).parent()
      select: (event, ui) =>
        $(@result_autocomplete).val(ui.item.id)
ai commented 10 years ago

Yeap, problem is that $(@input_autocomplete).autocomplete returns object and CoffeeScript returns all last code line (like Ruby).

I recommend to use non-shorcut or to but false at end on shortcut.

I can fix this API only in next major version, when I will remove old API.

onemanstartup commented 10 years ago

It is minor issue, thanks.