2017-08-04 8 views
1

検索から取得した値に応じてテンプレートを変更できます。私はこのような変数の内容変更を考えた:InstantSearch.jsで実行時に別のテンプレートを選択

search.addWidget(
    instantsearch.widgets.hits({ 
    container: '#hits', 
    hitsPerPage: 12, 
    templates: { 
     empty: noResultsTemplate, 
     item: hitTemplate 
    }, 
    transformData: function (hit) { 
     if (hit.myVariable == true) { 
      this.templates.item = otherTemplateVariable; 
     } 
     return hit; 
    } 
    }) 
); 

をしかし、私はエラーを取得する:'Unable to get property 'templates' of undefined or null reference'問題は、あなたが持っていないどの機能transformDatathisし、そのポイントを参照しているように思わ

答えて

0

テンプレート属性にアクセスするには、オブジェクトを抽出しようとしてください:

var data = { 
    container: '#hits', 
    hitsPerPage: 12, 
    templates: { 
     empty: noResultsTemplate, 
     item: hitTemplate 
    }, 
    transformData: function (hit) { 
     if (hit.myVariable == true) { 
      //access the templates attribute 
      data.templates.item = otherTemplateVariable; 
     } 
     return hit; 
    } 
    }; 

search.addWidget(
    instantsearch.widgets.hits(data) 
); 
+0

試しましたが、新しい割り当てられたテンプレートは使用しません。一度 'templates'で宣言されると、それが使用されます:('テンプレート'リストから 'item'を削除してこれを確認し、elseステートメントに追加して、どちらも使用しません。 – user0187409

関連する問題