Handlebars.js: how does partials gets invoked in a helper? I got: TypeError: fn is not a function - handlebars.js

I am learning Handlebars. It still appears to be a mystery to me that how a partials gets invoked in a helper.
I read this tutorial: http://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers
From this example in the tutorial,
Handlebars.registerHelper("stripes", function(array, even, odd, fn) {
var buffer = "";
for (var i = 0, j = array.length; i < j; i++) {
var item = array[i];
// we'll just put the appropriate stripe class name onto the item for now
item.stripeClass = (i % 2 == 0 ? even : odd);
// show the inside of the block
buffer += fn(item); <!-- this is where a partials gets invoked -->
}
// return the finished buffer
return buffer;
});
it appears the partial is added and applied by Handlebars. However, I used this same approach in Handablebars 1.3.0 and 2.0 Alpha-2, it seems no longer the case. I always got the error:
TypeError: fn is not a function
buffer += fn(item);
I did quite online search and found a number tutorials, but none of them shows how partials is hooked up with a helper in version 1.3.0 or later.
Can someone help me out?
Thanks a lot!

OK. I believed I solved this problem. In v1.0 or later, Handlebars puts everything in a hash, not just fn. So, everything in the above post still is valid except these two lines:
Handlebars.registerHelper("stripes", function(array, even, odd, options)
...
buffer += options.fn(item);
Hope this helps someone else. Any confirmation is welcome. I feel bad that there is no direct example on this, at least I did not find it.
Regards.

Related

Somebody know best way how to controll Meteor.methods result from client-Side

I just wonder about Meteor.methods result stub. When i used Meteor.call, i tried handle result set but when result set out of range from Meteor.call area that return undefined. So I think if i use Session it can be solve. but sometime that way isn't work. So, if you guys, have you know best way to controll that topic, let me know your solutions!!
Thank you for comment, but I can't split up my logic. Because i want take javaScript library module from our old project-code. It had handle front-end of jQuery by html context for iframe. FirstTime, i tried figured out by use just iframe tag. but it had made issue for cross-domain. So i tried found solution about cross-domain on Meteor, but anywhere no solution. Therefore, i thinking how to solve it that issue and then i decided used Meteor.http! That was successed tool html.content from server-side and inside block of the callback function from client-side. but i can't hand over result out of callback function. i should give result of methods(iframe html content) to our old javascript-code... that's why i try that way! Any idea about this? i include javascript-code, it will help understanding.
...
drawSvg2: function(){
var objIFrame = $("#iFrame_"+m.canvasIdx()).contents();
var objList = objIFrame.find('.page');
var len = objList.length;
for (i=0; i<len; i++){
var obj = objList[i];
var children = obj.children; //childNodes;
var num = 0;
for (var k = 0; k < children.length; k++) {
var children2 = children[k].children;
for (var j = 0; j < children2.length; j++) {
if(children2[j].children[0].nodeName == "P"){
m.textArray(num, new Object());
c.canvasFetch(children[k], children2[j], "", m.textArray()[num], num);
}else{
m.textArray(num, new Object());
c.canvasFetch(children[k], children2[j], children2[j].children[0], m.textArray()[num], num);
}
num++;
}
}
}
v.drawSvg3();
},
...
That is huge function and handled front-end by jQuery, So i can't put inside Meteor.methods..
The way to use Meteor.call:
Meteor.call('methodName', param1Value, param2Value,..., function (error, result) { ... } );
The last parameter is a callback. Meteor invokes this callback function when the result is ready. Inside the body of the callback function, you can do whatever you want with the result. Why do you need to return this result to the calling function? Can you split up the logic so that it invoke Meteor.call, and handle the rest inside the callback?

OpenCV 3 Tracker won't work after reinitialization

I have issue using OpenCV 3 tracking module for tracking. It behaves same, either I use interface class (cv::Tracker) or class with implementation (like cv::TrackerMedianFlow or cv::TrackerMIL, etc). Sample is a bit modified sample from OpenCV sample folder
After correct creation
Ptr<Tracker> tracker = Tracker::create( tracker_algorithm );
if ( tracker == NULL )
{
std::cout << "***Error in the instantiation of the tracker...***\n";
return -1;
}
initialization works just fine
if ( !tracker->init( frame, boundingBox ) )
{
std::cout << "***Could not initialize tracker...***\n";
return -1;
}
Problem occurs late on, withing main loop, when tracking is lost. I use separate detector for defining new target. When I find new target, I clear tracker and initialize it with new value
tracker->clear( );
if ( !tracker->init( frame, detectedNewBBox) )
{
std::cout << "***Could not initialize tracker without history...***\n";
return -1;
}
However, initialization always returns false. I am trying to find out WHY tracker cannot be initialized?
Data was check few time, and looks pretty correct. I even conducted small experiment, trying to initialize tracker right after creation with same data it won't initialize withing loop and it works perfect.
Am I doing something wrong? I was unable to find any documentation on this...
Here is link to available documentation on this:
OpenCV 3 Tracker documentation
Thanks for any effort!
I just ran into the same problem, here's how I got it to work :
tracker->clear();
Ptr<Tracker> trackerNew = Tracker::create( tracker_algorithm );
tracker = trackerNew;
tracker->init( image, boundingBox );
Might not be the right way or the prettiest but it does the job :)
If you want to track a new ROI (region of interest) then I suggest that you should create a new tracker instead of clearing and trying to reuse a previous tracker. Re-use when you need to call init will provide no extra benefit. As you have observed, re-initializing a tracker is not allowed by default.
But if you want to resume tracking of the same object with your correction, it might be possible by doing following steps (I have not tried it myself yet. following code is just a pseudo code)
Ptr<TrackerModel> model = tracker->getModel();
Ptr<TrackerTargetState> lastTargetstate = getLastTargetState();
// Make changes to lastTargetState (update position etc)
// Set lastTargetState, I am not sure if you need to actually set it
// or just editing the object through pointer should work.
model->setLastTargetState(lastTargetstate);
I ran into the same problem and here's my solution:
Open the file in opencv_contrib/modules/tracking/src/tracker.cpp and apply the following changes:
- if( isInit )
+ /*if( isInit )
{
return false;
}
+ */
I recompiled opencv3 and reinstalled it. That fixed it for me. I think they did not want people to reinitialize the tracker for some reason. I am not sure why?

InDesign SDK : Drag'n'Drop from a Flex Panel

I have a Flex panel, in InDesign, from which I drag an URL. If I drop this URL on a text editor or a web browser, it works. But when I try to drop it on my InDesign document, it's a little bit harder.
I have implemented a subclass of CDragDropTargetFlavorHelper. The drop works perfectly on Windows. But on mac, I have problems in the method CouldAcceptTypes :
DragDrop::TargetResponse AutocatDNDCustomFlavorHelper::CouldAcceptTypes(const DragDropTarget* target, DataObjectIterator* dataIter, const IDragDropSource* fromSource, const IDragDropController* controller) const
{
if (0 != dataIter && 0 != target)
{
DataExchangeResponse response = dataIter->FlavorExistsWithPriorityInAllObjects(kURLDExternalFlavor);
if (response.CanDo())
{
...
}
}
}
The problem is that response.canDo() answers kTrue on Windows, but kFalse on Mac. I tried to explore the content of dataIter, but a call on dataIter->First() returns nil. I tried a controller->GetItemCount(), which returns 1. But if I try a controller->GetDragItem(1), I get a nil pointer. I have the impress there is no item. Though, the drop works on another app than InDesign, as I said.
Is it a problem of internalization ? Or something else ? It let me dry.
Thanks in advance
-------------------------- EDIT -----------------------------------
I solved this problem, but discovered another one. The flavor sent by the flex panel has been changed, so that it's a text flavor instead of an URL flavor. My method couldAcceptType works now :
DragDrop::TargetResponse AutocatDNDCustomFlavorHelper::CouldAcceptTypes(const DragDropTarget* target, DataObjectIterator* dataIter, const IDragDropSource* fromSource, const IDragDropController* controller) const
{
if (0 != dataIter && 0 != target)
{
// Check for URL Flavor in the drag
DataExchangeResponse response = dataIter->FlavorExistsWithPriorityInAllObjects(kTEXTExternalFlavor);
if (response.CanDo())
{
return DragDrop::TargetResponse(response, DragDrop::kDropWillCopy);
}
}
return DragDrop::kWontAcceptTargetResponse;
}
The problem is now in the ProcessDragDropCommand method. Here is the code :
ErrorCode AutocatDNDCustomFlavorHelper::ProcessDragDropCommand(IDragDropTarget* target, IDragDropController* controller, DragDrop::eCommandType action)
{
// retrieve drop data
IPMDataObject* dragDataObject = controller->GetDragItem(1);
uint32 dataSize = dragDataObject->GetSizeOfFlavorData(kTEXTExternalFlavor) ;
...
}
The problem is the IMPDataObject I get is nil. There is no item in the controller. However, there were items in the CouldAcceptTypes method, in the DataObjectIterator. So, where are my items ?
I tried using a custom CDataExchangeHandlerFor, but could not really understand what its usage was for. It didn't work anyway.
Has anyone an idea ?
Regards,
RĂ©mi
The problem is the argument of the GetDragItem. It is 1 on PC. It is a strange value on Mac (something like 719853). The only dirty solution I found is doing a memcpy from the object retrieved drom the dataIter in the CouldAcceptTypes method, and use it in the ProcessDragDropCommand method.

Getting characters position in TLF

i am trying to figure out if i can in anny way get the exact position for every character inside a textflow?, also I'm having trouble with the TLF documentation, is there anny documentation that shows more on how to work with it in actionscript rather than mxml, im looking to write my own component and perhaps not use richtexteditor if i don't need to.
Many thanks!
Edit: i finally think i figured out how to get each characters position in the textflow:
private function getCharPosition():void {
for (var i:int=0; i<=textController.flowComposer.numLines; i++) {
var textFlowLine:TextFlowLine = textController.flowComposer.findLineAtPosition(i);
var textLine:TextLine = textFlowLine.getTextLine();
trace('number of atoms in this line: ' + textline.atomCount);
for (var j:int=0; j<=textLine.atomCount; j++) {
try {
trace(textLine.getAtomBounds(j));
} catch (e:Error) {
trace('error');
}
}
}
}
This returns an error that's why i have try and cache, i have tried to change textLine.atomCount to -1 but that wont work either. Now i don't know what character exactly that i have the position for. Lots of figuring out yet...
You can use some of these steps which (among other things) allow to determine character's bounds.
What about your second question you can refer to the following documentation and/or some samples.

.toggle(true) throw null in $(document).ready(function())

I am toggling row siblings. I wrote .toggle(true) when document ready. see below picture. I think row sibling are not availble before this function calls.
$(document).ready(function() {
$('tr[#class^=RegText]').hide().children('td');
list_Visible_Ids = [];
var idsString, idsArray;
idsString = $('#myVisibleRows').val();
idsArray = idsString.split(',');
$.each(idsArray, function() {
if (this != "") {
$(this).siblings('.RegText').toggle(true);
list_Visible_Ids[this] = 1;
}
});
How to resolve this? why sliblings are not avaible in when document is ready?
Your posted code doesn't match the debugger code, your code has this, which is (almost!) correct:
$(this).siblings('.RegText').toggle(true);
The debugger has this, which is incorrect:
$(this).siblings(('.RegText').toggle(true));
You need to update whatever you're actually debugging to that code without the extra parenthesis, otherwise you're going to get some pretty funky behavior there.
Also you need a # in there since your debugger shows you're not storing the hash mark in the array, which is perfectly fine. You're currently calling $("row10") (which looks for <row10> elements), but what you need is $("#row10") (which looks for id="row10" elements), so adjust your call like this:
$('#' + this).siblings('.RegText').toggle(true);

Resources