在自定义块中包装时,Helper会丢失父上下文

时间:2016-02-24 15:43:33

标签: javascript handlebars.js handlebarshelper

我有一个Handlebars块正常呈现,直到我将它包装在一个自定义块中。具体来说,调用父上下文来获取extension FolderContent { @NSManaged var contentID: NSNumber? @NSManaged var contentTitle: String? @NSManaged var contentType: String? @NSManaged var publishDate: String? @NSManaged var folderList: NSSet? } extension FolderList { @NSManaged var folderID: NSNumber? @NSManaged var folderName: String? @NSManaged var folderDetail: NSSet? } func updateFolderList() { // Initialize Fetch Request let fetchRequest = NSFetchRequest() // Create Entity Description let entityDescription = NSEntityDescription.entityForName(FOLDER_LIST, inManagedObjectContext: self.managedObjectContext) // Configure Fetch Request fetchRequest.entity = entityDescription do { let result = try self.managedObjectContext.executeFetchRequest(fetchRequest).last as! FolderList let content = result.mutableSetValueForKey("folderDetail") content.addObject(self.getContent()) var folderContent:FolderContent = result.folderDetail?.allObjects.first as! FolderContent print(folderContent.contentTitle) self.save() print(result) } catch { let fetchError = error as NSError print(fetchError) } } func getContent()->FolderContent { let folderContent = NSEntityDescription.insertNewObjectForEntityForName(FOLDER_CONTENT, inManagedObjectContext: self.managedObjectContext) as! FolderContent folderContent.contentID = 1 folderContent.contentTitle = "Sandeep" folderContent.contentType = "Product" return folderContent } 类型。请记住,此Handlebars块包含在Currency

each

很清楚我知道问题出在哪里,我只是不确定如何解决它。

以下是Handlebars块:

{{#each this.SubscriptionOptions.MonthlySubscriptions}}

现在失败的作品是:

<p class="lead" style="font-size:40px">
  {{#ifGreaterThanZero PricePerBillingPeriod}}
    <strong>{{currency ../Currency}}{{priceFormat PricePerBillingPeriod}}</strong>
  {{else}}
    <strong>FREE</strong>
  {{/ifGreaterThanZero}}
</p>

以下是{{currency ../Currency}} 帮助程序代码:

ifGreaterThanZero

我查看了Handlebars.registerHelper('ifGreaterThanZero', function(value, options) { var intVal = parseInt(value); if (intVal) { return options.fn(this); } else { options.inverse(this); } }); ,它实际上是订阅选项本身,因此它包含this

问题是,如何获得它以便再次访问父上下文?

1 个答案:

答案 0 :(得分:0)

好的,所以我明白了。你必须上升一级。它不一定是对象意义上的父上下文,它是帮助父上下文,所以你越深入,它就像目录结构一样向上移动。

新代码如下所示:

<strong>{{currency ../../Currency}}{{priceFormat PricePerBillingPeriod}}</strong>