Custom widget talking to my controller.

70 views
Skip to first unread message

Foxandxss

unread,
Sep 2, 2011, 8:48:30 AM9/2/11
to angular
Hello, I saw how Igor Minar implemented a contenteditable widget
(https://github.com/angular/angular.js/issues/528)

That works perfect. But... how to save the changes? I Could implement
a save function in my controller that send the edited element to my
server.

But how? I edit my element, the widget code will be executed but
nothing more. So I think that the widget need to notify my controller
that some element have changed and save it. Am I correct?

I guess that the "instance" parameter is the element that is being
changed.

Another thing... The contenteditable widget is used in an element
inside a template, so I use it like...

ngx:contenteditable="item.element"

will the widget have information about the item itself or just the
element property?

I mean, if I could make the widget talk to the controller, I need to
give some information to the controller to specify what item I'm
editing. In other words, would be good if the controller can send the
entire item to the controller so I can get the ID and the edited
content from that item and send it to my server.

Igor Minar

unread,
Sep 3, 2011, 6:25:33 PM9/3/11
to ang...@googlegroups.com
On Fri, Sep 2, 2011 at 5:48 AM, Foxandxss <foxa...@gmail.com> wrote:
Hello, I saw how Igor Minar implemented a contenteditable widget
(https://github.com/angular/angular.js/issues/528)

I think that it was Misko who implemented this. I just posted the code to the issue. :-)
 

That works perfect. But... how to save the changes? I Could implement
a save function in my controller that send the edited element to my
server.

But how? I edit my element, the widget code will be executed but
nothing more. So I think that the widget need to notify my controller
that some element have changed and save it. Am I correct?

see below..
 

I guess that the "instance" parameter is the element that is being
changed.

Another thing... The contenteditable widget is used in an element
inside a template, so I use it like...

ngx:contenteditable="item.element"

will the widget have information about the item itself or just the
element property?

both
 

I mean, if I could make the widget talk to the controller, I need to
give some information to the controller to specify what item I'm
editing. In other words, would be good if the controller can send the
entire item to the controller so I can get the ID and the edited
content from that item and send it to my server.

you are thinking about this incorrectly. you never want the controller to know anything about DOM. that's the whole idea of separating the template from the code driving the view.

What you want is to set up a watch in your controller to watch model that contenteditable is modifying.

<div ngx:contenteditable="model"></div>

function MyCtrl() {
  this.$watch('model', function(newVal) {
    //save the newVal
  });
}

I think you'll also want to modify the contenteditable widget to only update the model on blur, because otherwise you'll end up saving the model on each keystroke.

/i

Foxandxss

unread,
Sep 3, 2011, 7:04:51 PM9/3/11
to angular
Thank you Igor, you always help a lot :)

I like AngularJS but is quite hard for me.

The watch on the model should work, the problem is that 'model' is an
array of data and if I send all the data to the server for just one
change... you know, slow.

Ill find the way.

Thanks again.

On Sep 4, 12:25 am, Igor Minar <i...@angularjs.org> wrote:
Reply all
Reply to author
Forward
0 new messages