I had x-editable working in Meteor 0.7.2 but since upgrading to 0.8.0 it no longer renders correctly. I tend to end up with a bunch of Empty tags. This is frustrating because the data is there, just not by the time the rendered function is fired.
<template name="clientPage">
<header>{{> clientPageTitleUpdate}}</header>
</template>
<template name="clientPageTitleUpdate">
<h1><span class="title-update editable" data-type="text" data-pk="{{_id}}" data-name="title" data-value="{{title}}">{{title}}</span></h1>
</template>
Template.clientPageTitleUpdate.rendered = function() {
console.log(this.$(".title-update").text());
// set up inline as defaule for x-editable
$.fn.editable.defaults.mode = 'inline';
$(".title-update.editable:not(.editable-click)").editable('destroy').editable({
url: "empty",
toggle: "dblclick",
success: function (response, newValue) {
// update value in db
var currentClientId = $(this).data("pk");
var clientProperties = { title: newValue };
Clients.update(currentClientId, {$set: clientProperties}, function(error) {
if (error) {
Errors.throw(error.message)
}
});
}// success
});
}
I have tried the "new" rendered method of embeding the this into another template as explained here and it doesn't seem to work either.
What is the best way to use x-editable now that rendered only fires once and doesn't make sure the data is there.
I am using Iron Router and my templates are not embeded in an {{#each}} block which seems to be the basic solution to the new rendered model.
This question is related to this older topic about x-editable in a meteor template.
Any help whatsoever would be super appreciated here. I am at a loss. Thanks
If Andrew's answer worked for you, and you have a lot of fields like this, you may find convenient to use a function to create the required templates. Here is an example
and in the js:
EDIT: Much easier to implement now in Meteor 0.8.3:
Template:
Code:
For this to be most efficient, make sure the data context of the editable template is only the field being edited, as in the example above with
{{> docTitle someHelper}}
.Outdated information follows for Meteor 0.8.0 to 0.8.2
I also had to do this but wasn't sure about using the global helper in my app. So I tried to accomplish it by just changing the behavior of the editable.
The main things that needed to be done, after perusing the docs and source, were:
display
function so that reactivity of text updated by Meteor doesn't breakHere's the code (apologies for Coffeescript):
This is ugly because it destroys and re-creates the popover after setting a new value, so that the form field updates from the correct value.
After some more reverse engineering, I found a cleaner way to do this that doesn't involve destroying the editable. Gadi was right on that
container.data().editableContainer.formOptions.value
has something to do with it. It's because this value is set after the update because x-editable thinks it can cache this now. Well, it can't, so we replace this with the original function so the value continues being updated from the text field.Notes:
$.trim
above was taken from the default behavior to render valueI will try to make this more concise in the future pending better support from Meteor for depending on data reactively.
This is my simplified approach, based on gadicc's post (tested with Meteor 0.9.3).
Let's say there is a
MyDocuments
collection, which is rendered viadocumentList
template. Each document in the collection has thetitle
field, which we want to edit using xedtiable.document.html
document.js
xeditable.html
xeditable.js
Updated for Meteor 0.8.3+
This covered all cases for me (see below the code). This uses pretty fine-grained reactivity and will update the x-editable instance only when the specified value changes.
Template:
Client Javascript (for Meteor 0.8.3+):
You can see it in action at http://doingthiswithmeteor.com/ (with two windows open). You need to be logged in, but try change any of your info on the "me" page.
Just implemented this... still doing some testing but feedback welcome. This replaces my previous helper workaround.
Working off of Andrew's answer, I was able to get this to work for me. It's not in coffeescript, also I think the Blaze.getCurrentData() might now be Blaze.getData() as per the Meteor docs.
Template:
Code:
There are probably improvements I can make and I'm happy to hear them, but I spent a lot of time dealing with bad coffeescript translation (kept telling me to use return all the time), so I wanted to add another example.
Yet another implementation working with iron-router and managing Collection2 validation:
The control
And the JS code:
I couldn't find a way to set the data context automatically. I'm sure it shouldn't be very difficult. Any help welcome!