adammark/Markup.js

Potential gotcha: backtick expressions seem to be passed as strings

gregdyke opened this issue · 5 comments

I am using moment.js to format dates. I created a custom pipe to compare two dates (moment objects) (start and end on an event object), one of which gets passed as a backtick expression. The backtick expression was passed as a string.

Can I see the code for your custom pipe? Thanks.

My original code ("incorrect").

An alternative is to pass the parent context as an object, showing what the result ought to be ("correct").

diffday pipe gets called with an Object and a String

http://jsbin.com/faja/1/edit?html,js,console,output

Thanks, I'll play with this ASAP. In the meantime, a couple things.

First, I've tested the ability to process objects inside backticks. Not sure why moment would break this.

Second, have you considered setting a variable inside the model itself, rather than perform this business logic in the view? See my example in the docs, which I'll repeat here:

<!-- hard to read -->
{{if user.age|more>`user.retirement_age`}}

<!-- easy to read -->
{{if user.retired}}

Your test passes a number, which, even if cast to string by the backtick operator, will still work because you "cast" it to number in pipes.plus. Compare:

"2"+3
// => "23"
+"2"+3
// => 5

Also javascript treates Number as a special kind of Object. Maybe you could create a test with some kind of array operation, or a generic hashtable printer?

As for your suggestion, that's exactly what I did. Although "correctly" formatting a date interval depending on its number of "between" days, I would consider to be at the same level as pluralisation code.

Okay, I confirmed your result. Let me think about whether Markup should support this use case. In any event, I'll clarify the docs. Thanks.