How do I use X-editable on dynamic fields in a Meteor template now with Blaze?

前端 未结 6 2091
忘了有多久
忘了有多久 2021-01-01 05:42

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 becaus

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 05:54

    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+):

    // once off for your entire project
    Template.xedit.rendered = function() {
      var container = this.$('*').eq(0);
      this.autorun(function() {
        var value = Blaze.getCurrentData().value;
        var elData = container.data();
        if (elData && elData.editable) {
          elData.editable.setValue(value, true);
          // no idea why this is necessary; xeditable bug?
          if (elData.editableContainer)
            elData.editableContainer.formOptions.value = elData.editable.value;
        }
      });
    }
    
    // per instance; change selector as necessary
    Template.userInfo.rendered = function() {
      // Note you don't need all the :not(.editable) stuff in Blaze
      this.$('a').editable({
        success: function(response, newValue) {
          // according to your needs
          var docId = $(this).closest('[data-user-id]').attr('data-user-id');
          var query = { $set: {} }; query['$set']['profile.username'] = newValue;
          Meteor.users.update(docId, query);
        }
      });
    });
    

    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.

    1. Setup x-editable in rendered() as usual
    2. Ability for custom display functions, "empty" values, etc.
    3. Open in two windows. Change value in win1, click on value in win2.. the POPUP should display the correct value.
    4. Support for custom types like dates and arrays from custom helpers

    Just implemented this... still doing some testing but feedback welcome. This replaces my previous helper workaround.

提交回复
热议问题