bminer / node-blade

Blade - HTML Template Compiler, inspired by Jade & Haml
Other
320 stars 28 forks source link

Meteor : reactive helper using Session.equals #159

Closed vraptor75011 closed 11 years ago

vraptor75011 commented 11 years ago

I m trying to trigger an in place editing feature this way

post.coffee

Template['posts/post'].helpers
   editing: ->
      Session.equals("POST_EDITING", this.post._id)

post.blade

- if(post.editing)
    p    form for in place editing

This doesn't work, the template is not rendered when the Session variable is changed

If I change my post.blade to

-if(Session.equals("POST_EDITING", post._id)
   p   form for in place editing

Everything works fine.

Is it a bug with node-blade losing reactivity when using Session.equals in a helper ?

bminer commented 11 years ago

It could be that this.post is not available within your view helper. Try adding changing your helper to the following:

Template['posts/post'].helpers
   editing: ->
      console.log("this.post", this.post);
      console.log("this", this);
      Session.equals("POST_EDITING", this.post._id)

... and then post the output printed to the console. You can also email a screenshot to me if you like.

bminer commented 11 years ago

Also, could you post the entire template post.blade (or at least the part where post is defined)

vraptor75011 commented 11 years ago

I dont think I have a problem with the definition of post in my view. The first time the post.blade is rendered, the helper works fine. Then, if i click on post-edit, I trigger the session variable POST_EDITING to the post_id and then, the view is not rerended as it should (as if post.editing is not evaluated live).

post.coffee

Template["posts/post"].events =
  "click #post-delete": (e, instance) ->
    post = instance.data.post
    console.log post, this._id
    Posts.remove post._id
  "click #post-edit": (e, instance) ->
    post = instance.data.post
    console.log post, this._id
    Session.set("POST_EDITING", post._id)
  "click #post-comment": (e, instance) ->
    post = instance.data.post
    console.log post, this._id
    Session.set("POST_SHOW_COMMENTS", post._id)
  "keyup #edit-post": (e, instance) ->
    console.log("Je tape")
    $form = $("#edit-post")
    $title = $("#title", $form)
    $body = $("#body", $form)
    console.log($title.val())
    Posts.update({_id: Session.get('POST_EDITING')}, {title: $title.val(), body: $body.val()})
    console.log("update")
Template["posts/post"].helpers =
  "editing": ->
    console.log("this.post", this.post)
    console.log("this", this)
    Session.equals("POST_EDITING", this.post._id)

post.blade (working)

div.row
    div.span1
        div.btn-toolbar
            div.btn-group
                a#post-edit.btn.btn-inverse.disabled(href="#")
                    i.icon-white.icon-pencil
                a#post-comment.btn.btn-inverse.disabled(href="#")
                    i.icon-white.icon-comment
        div.btn-toolbar
            div.btn-group
                a#post-delete.btn.btn-inverse.disabled(href="#")
                    i.icon-white.icon-trash
    div.span8
        -if(Session.equals("POST_EDITING", post._id))
            #edit-post
                form.form-horizontal
                    fieldset
                        legend New Post
                        div.control-group
                            label Title
                            div.controls
                                input#title(type="text", value="#{post.title}", placeholder="Type the title ...")
                        div.control-group
                            label Body
                            div.controls
                                textarea#body(rows=3, placeholder="Type the post ...")
                                    =post.body
                        div.control-group
                            label Bouton
                            div.controls
                                button.btn.btn-primary Post
        -else
            h1
                = post.title
            p
                = post.body
            - if(Session.equals("POST_SHOW_COMMENTS", post._id))
                include '../comments/comments' exposing post

post.blade (not working)

div.row
    div.span1
        div.btn-toolbar
            div.btn-group
                a#post-edit.btn.btn-inverse.disabled(href="#")
                    i.icon-white.icon-pencil
                a#post-comment.btn.btn-inverse.disabled(href="#")
                    i.icon-white.icon-comment
        div.btn-toolbar
            div.btn-group
                a#post-delete.btn.btn-inverse.disabled(href="#")
                    i.icon-white.icon-trash
    div.span8
        -if(post.editing)
            #edit-post
                form.form-horizontal
                    fieldset
                        legend New Post
                        div.control-group
                            label Title
                            div.controls
                                input#title(type="text", value="#{post.title}", placeholder="Type the title ...")
                        div.control-group
                            label Body
                            div.controls
                                textarea#body(rows=3, placeholder="Type the post ...")
                                    =post.body
                        div.control-group
                            label Bouton
                            div.controls
                                button.btn.btn-primary Post
        -else
            h1
                = post.title
            p
                = post.body
            - if(Session.equals("POST_SHOW_COMMENTS", post._id))
                include '../comments/comments' exposing post
bminer commented 11 years ago

Kinda weird. What's the output to the console when the view is initially rendered? How about when it's supposed to be re-rendered? Any output then? Do you have a link to your project so that I can do some debugging? Or, can you send me a screenshot of the console?

The reason why I ask is that Blade uses some funky stuff to get helpers to work. I'm betting that the problem lies somewhere in there...

Thanks for the bug report. We'll get this one fixed up.

vraptor75011 commented 11 years ago

My test project : https://github.com/vraptor75011/MeteorBladeBlog.git

bminer commented 11 years ago

That's a big help. Thanks. I'll fork the repo and investigate as soon as I get some free time.

vraptor75011 commented 11 years ago

Thanks

bminer commented 11 years ago

I keep getting the following:

Running using Meteor 0.6.2 (latest)

=> Meteor server running on: http://localhost:3000/
Exception from sub aQxaRTSnqwhSTBY88 ReferenceError: Comments is not defined
    at null._handler (app/server/lib/publications.coffee.js:20:10)
    at _.extend._runHandler (app/packages/livedata/livedata_server.js:815:31)
    at _.extend._startSubscription (app/packages/livedata/livedata_server.js:714:9)
    at _.extend.protocol_handlers.sub (app/packages/livedata/livedata_server.js:520:12)
    at _.extend.processMessage.processNext (app/packages/livedata/livedata_server.js:484:43)
Exception from sub NevBooA7GBkXCXfqa ReferenceError: Posts is not defined
    at null._handler (app/server/lib/publications.coffee.js:3:10)
    at _.extend._runHandler (app/packages/livedata/livedata_server.js:815:31)
    at _.extend._startSubscription (app/packages/livedata/livedata_server.js:714:9)
    at _.extend.protocol_handlers.sub (app/packages/livedata/livedata_server.js:520:12)
    at _.extend.processMessage.processNext (app/packages/livedata/livedata_server.js:484:43)
vraptor75011 commented 11 years ago

Updated project for Meteor > 0.6 : git://github.com/vraptor75011/MeteorBladeBlog2.git

Apparently this issue is due to a new feature in Meteor : "We've added file-level JavaScript variable scoping. Variables declared with var at the outermost level of a JavaScript source file are now private to that file. Remove the var to share a value between files."

In coffee files, il I write : "@Posts = new Meteor.Collection ..." I can access the collection.

bminer commented 11 years ago

I cannot replicate any bugs... probably because my Mongo DB is empty. Could you provide me with some sample values that I can load into the DB? Or, can you simplify this bug into a simple example that I can run and test?

Thanks for your help.

vraptor75011 commented 11 years ago

Had an error in my coffee file defining the helpers

Template["posts/post"].helpers =
  "editing": ->
    console.log("this.post", this.post)
    console.log("this", this)
    Session.equals("POST_EDITING", this.post._id)

Should be

Template["posts/post"].helpers
  "editing": ->
    console.log("this.post", this.post)
    console.log("this", this)
    Session.equals("POST_EDITING", this.post._id)
bminer commented 11 years ago

Hm, that's strange. I thought that either syntax would work...

Glad that the issue is solved now.