brianchance / knockout-x-editable

knockout binding handler for x-editable
74 stars 22 forks source link

Fire function instead of url on save? #15

Closed andreaslarssen closed 10 years ago

andreaslarssen commented 10 years ago

Do you have an example on how to run a function in your viewmodel on save (instead of setting a url in editableOptions)? I want my service to handle the requests.

brianchance commented 10 years ago

How would x-editable handle it?

On Tue, Jun 17, 2014 at 1:56 PM, halloverden notifications@github.com wrote:

Do you have an example on how to run a function in your viewmodel on save (instead of setting a url in editableOptions)? I want my service to handle the requests.

— Reply to this email directly or view it on GitHub https://github.com/brianchance/knockout-x-editable/issues/15.

andreaslarssen commented 10 years ago

x-editable seems to suggest you use jquery on the element. What I want to do is bind the url to a function in my viewmodel.

brianchance commented 10 years ago

So you want the url to be ko.observable?

On Tue, Jun 17, 2014 at 2:29 PM, halloverden notifications@github.com wrote:

x-editable seems to suggest you use jquery on the element. What I want to do is bind the url to a function in my viewmodel.

— Reply to this email directly or view it on GitHub https://github.com/brianchance/knockout-x-editable/issues/15#issuecomment-46353812 .

andreaslarssen commented 10 years ago

Not really. I want to run a method in my viewmodel that handles the ajax request through a service, instead of specifying the url directly in editableOptions. It would make it easier to manage env specific URLs, as well as allowing me to have all my services in one place instead of calling some functions in the view, and some from the service(s).

brianchance commented 10 years ago

The binding handler just passes everything through to x-editable, so the url could be a function that takes a params and returns a deferred to resolve. Take a look at their docs.

I am guessing this would work:

On Tue, Jun 17, 2014 at 2:41 PM, halloverden notifications@github.com wrote:

Not really. I want to run a method in my viewmodel that handles the ajax request through a service, instead of specifying the url directly in editableOptions. It would make it easier to manage env specific URLs, as well as allowing me to have all my services in one place instead of calling some functions in the view, and some from the service(s).

— Reply to this email directly or view it on GitHub https://github.com/brianchance/knockout-x-editable/issues/15#issuecomment-46355148 .

brianchance commented 10 years ago

I would also guess you could set it globally for the page using $.fn.editable.defaults.url.

andreaslarssen commented 10 years ago

Yeah, I tried setting a VM function which returns a deferred as a URL. Does not seem to work. At least I am not able to get it to work. I'll take another look at it if you think it should work.

brianchance commented 10 years ago

The binding handler does not do anything with the url besides pass it to x-editable. If you can get it working without the binding handler, send me the code. Make sure you pass the function, not the result of the function - url: myfunc, not url: myfunc()

On Tue, Jun 17, 2014 at 2:50 PM, halloverden notifications@github.com wrote:

Yeah, I tried setting a VM function which returns a deferred as a URL. Does not seem to work. At least I am not able to get it to work. I'll take another look at it if you think it should work.

— Reply to this email directly or view it on GitHub https://github.com/brianchance/knockout-x-editable/issues/15#issuecomment-46356276 .

brianchance commented 10 years ago

I realized you will probably need to set the property as $data.myfunc or $parent.myfunc so it can find it.

andreaslarssen commented 10 years ago

Passing the function, not the result. It finds the functions, as it runs when using (), but still no luck.

brianchance commented 10 years ago

Can you post your binding?

On Tue, Jun 17, 2014 at 4:23 PM, halloverden notifications@github.com wrote:

Passing the function, not the result. It finds the functions, as it runs when using (), but still no luck.

— Reply to this email directly or view it on GitHub https://github.com/brianchance/knockout-x-editable/issues/15#issuecomment-46367232 .

andreaslarssen commented 10 years ago
<span data-bind="editable: user().firstName, editableOptions: { type: 'text', url: $root.save}"></span>
brianchance commented 10 years ago

This is the same binding I have used, the save method should be called, is that correct? Put a break point or log in the method. Change the content and save it, should get called.

If it is called, then you will have to work out exactly what to call/return with x-editable.

If it is not called, send me a bigger example (or jsfiddle,plunker) where I can see the view model and page.

andreaslarssen commented 10 years ago

You've already seen the view:

<span data-bind="editable: user().firstName, editableOptions: {type: 'text', url: $root.save}"></span>

Here's the relevant chunk of the view model:

var app = app || {};

app.viewModel = function(userService) {
    var self = this;

    self.save = function() {
        console.log('save');
    }
}

I can bind to other functions in the same view model, so it's not the knockout setup. Are you able to reproduce this, or more important; are you able to use x-editable with a view model function in another project?

andreaslarssen commented 10 years ago

What knockout version is it depending on?

brianchance commented 10 years ago

Right now I am using knockout 3.1.0 and x-editable 1.4.4.

brianchance commented 10 years ago

Take a look at this plunker, shows it working... http://plnkr.co/edit/Epf1k6Tr4Rq2rfpGPMef

Shows passing it on the editableOptions, you can also set the default and not specify on each one (line 20)

andreaslarssen commented 10 years ago

Sorry for not having closed this before. I left my code, and just picked it up again. And waddayouknow, it works! I have no idea why or how, the only explanation I have is that the cache must have played me a trick. Thanks for being patient and helping me out.