simple widget example; bind keyup

410 views
Skip to first unread message

hookerc...@gmail.com

unread,
May 22, 2011, 6:05:22 PM5/22/11
to ang...@googlegroups.com
basically I am adding a widget to a input; which basically allows for something like this

<input id="new-todo" name="todo.title" ng:insertNewLine="addTodo()" placeholder="What needs to be done?" type="text">

note this is in coffee script as it is my poison

angular.widget "@ng:insertNewLine", (expression, element) ->
  (element) ->
    scope = @
    element.bind "keyup", (event) ->
      if event.keyCode == 13
        scope.$tryEval(expression, element)
        event.stopPropagation() 

The problem is that the "todo" is undefined; in the addTodo function; if I use ng:click the todo will be an object with the title set;

Cheers

Misko Hevery

unread,
May 22, 2011, 8:36:33 PM5/22/11
to ang...@googlegroups.com
That is because your widget prevents the angular's input widget from running. 

Any reason why you are using angular.widget instead of angular.directive('ng:insertNewLine', ..)? I think if you switch to directive it should work. (note the directive does not have the @ prefix)

--
You received this message because you are subscribed to the Google Groups "angular" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Richard Hooker

unread,
May 23, 2011, 6:20:45 AM5/23/11
to ang...@googlegroups.com
thanks for reply; 

I now have 

angular.directive "ns:insertnewline", (expression, element) ->
  (element) ->
    scope = @
    element.bind "keyup", (event) ->
      if event.keyCode == 13
        scope.$tryEval(expression, element)
        scope.$root.$eval()

which works fine; is there anything that you would recommend not doing; or doing from this code snippet; 

I not 100% on the whole scope.$root.$eval(); should I not be able to eval just on the scope??? it seems to be that calling updateView would make more sense syntax wise here(if I can how do I from this function?); What is the best procedure for updating a view?

I originally choose widgets as I thought I required my widget to take precedence over the directive ones; to be honest I dont really understand the difference; the docs say precedence and dom manip which I thought my widget did;

also why would creating the widget prevent the angular's input from running exactly??

Also I have tried endless client libraries sproutcore backgone etc etc; Angular is approach is really cool; kind of strange to get ur head round at first as is different from what I have used before; But its "winning"

thanks 



Richard Hooker

unread,
May 23, 2011, 6:44:20 AM5/23/11
to ang...@googlegroups.com
sorry being a pain; 

The example application that I am building for a blog post is a todos application; pretty much mimicking the new sproutcore one; 

I create a todo; push the todo onto the controllers todos; this works great however; all the todos seem to be bound to the new one;

as shown the the pic; any ideas??



<div id="todoList" ng:controller="TodosController" class="ng-directive">
        <h2>
          Todos
        </h2>
        <input id="new-todo" name="todo.title" ng:insertnewline="addTodo(todo)" placeholder="What needs to be done?" type="text">
        <div id="stats">
          <span ng:bind="remaining" class="ng-directive ng-binding"></span>
        </div>
        <ul>
          <!-- ng:repeat: todo in todos -->
        </ul>
      </div>


Screen shot 2011-05-23 at 11.42.41.png

Misko Hevery

unread,
May 23, 2011, 4:11:29 PM5/23/11
to ang...@googlegroups.com
On Mon, May 23, 2011 at 3:20 AM, Richard Hooker <hookerc...@gmail.com> wrote:
thanks for reply; 

I now have 

angular.directive "ns:insertnewline", (expression, element) ->
  (element) ->
    scope = @
    element.bind "keyup", (event) ->
      if event.keyCode == 13
        scope.$tryEval(expression, element)
        scope.$root.$eval()

which works fine; is there anything that you would recommend not doing; or doing from this code snippet; 

I not 100% on the whole scope.$root.$eval(); should I not be able to eval just on the scope??? it seems to be that calling updateView would make more sense syntax wise here(if I can how do I from this function?); What is the best procedure for updating a view?

this API is already rewritten and will be replaced in next release, so I don't want to go into it to deep.
 

I originally choose widgets as I thought I required my widget to take precedence over the directive ones; to be honest I dont really understand the difference; the docs say precedence and dom manip which I thought my widget did;

We are planning to have just directives, to simplify this.
 

also why would creating the widget prevent the angular's input from running exactly??

because @widget has priority over widget, and unless you do some extra stuff which tells angular to continue compiling it prevents the default.
 

Also I have tried endless client libraries sproutcore backgone etc etc; Angular is approach is really cool; kind of strange to get ur head round at first as is different from what I have used before; But its "winning"


Glad to heard that!
 

thanks 

Misko Hevery

unread,
May 23, 2011, 4:14:17 PM5/23/11
to ang...@googlegroups.com
I would have to see the whole source to help you out. Could you share it?

Richard Hooker

unread,
May 24, 2011, 3:18:35 AM5/24/11
to ang...@googlegroups.com
yeah sure 

Yeah its the todo example application I am working on



app/assets/javascript

However I solved the problem via a copy; so obviously the input field has one object and when I pushed that object onto the array; I was always dealing with one object; so any adding text in the input field would be bound to the objects in the array therefore a cool looking multi update which was not intended; 

Cheers
Reply all
Reply to author
Forward
0 new messages