2012-10-29 30 views
1

我一直在寻找这个数小时,我不认为我能够看到解决方案。无法将数据从Sammy传递到淘汰赛模板

这是我router.js:

define('router', ['jquery', 'config', 'nav','store'], function ($, config, nav, store) { 

    var 
     concepTouch = Sammy('body', function() { 
     // This says to sammy to use the title plugin 
      this.use(Sammy.Title); 
      this.use(Sammy.Mustache); 
     // Sets the global title prefix 
     this.setTitle(config.title.prefix); 
     // So I can access sammy inside private methods 
     var sammy = this; 

     function establishRoutes() { 
      // Defines the main container for content then 
      var mainConainer = $(config.mainContentContainerId); 
      // Adds animation loading class to the main container 
      mainConainer.addClass(config.loadingAnimationCssClass); 
      // iterates through routes defined in config class then 
      _.forEach(config.appRoutes, function(obj, key) { 
       // defines each one as a route 
       sammy.get(obj.hashV, function(context) { 
        // Store the requested route as the last viewed route 
        store.save(config.stateKeys.lastView, context.path); 
        // Fetches its html template 
        context.render(obj.tmpltURL, { 'routeData': context.params }) 
         // Appends that htmlo template to the main container and removes loading animation 
         .then(function(content) { 
          mainConainer.removeClass(config.loadingAnimationCssClass).html(content); 
         }); 
        // Finally adds the route title to the prefix 
        this.title(obj.title); 
       }); 

       // Overriding sammy's 404 
       sammy.notFound = function() { 
        // toast an error about the missing command 
        toastr.error(sammy.getLocation() + ' Does not exist yet!'); 
        // Go to last visited anf if not 
        sammy.setLocation(
         store.fetch(config.stateKeys.lastView) || config.getDefaultRoute() 
        ); 
       }; 
      }); 
     } 
     // Calls for routes to be established 
     establishRoutes();   
     }), 
     // runs concep touch as a sammy App with the initial view of default route 
     init = function() {    
      // Try to get today's last visit and if not available then fallback on default 
      concepTouch.run(store.fetch(config.stateKeys.lastView) || config.getDefaultRoute()); 
      // Make the correct nav item active and add Click handlers for navigation menu 
      nav.setStartupActiveClass(store.fetch(config.stateKeys.lastView) || sammy.getLocation()) 
       .addActiveClassEventHandlers(); 
     };  
    return { 
     init: init, 
     concepTouch: concepTouch 
    }; 
}); 

这时候我提交搜索表单得到这个模板我:

<div id="contacts" class="view animated fadeInLeft"> 
    <h3>Search results for {{routeData}}</h3> 
    <ul data-bind="template: { name: 'searchresults-template', foreach: searchResults }"></ul> 
</div> 
<script type="text/html" id="searchresults-template"> 
    <li data-bind="text: type"></li> 
</script> 

<script> 
    require(['searchresults'], function (searchresults) { 
     searchresults.get(to Some how Get routeData.term); 
    }); 
</script> 

,我无法找到合适的方法,使胡须通数据从router.js context.render(obj.tmpltURL, { 'routeData': context.params })这一行的模板内部到{{routeData.term}}

{{routeData}} on its own returns `SAMMY.OBJECT: {"TERM": MY SEARCH TERM}` 

我不能导航到我想从它使用的属性。符号。而且即使工作不能被传递到JavaScript这是我真正需要的是

searchresults.init(); is waiting for this paramter `searchresults.init(routeData.term);` 

或者,也许答案是找到一种方法来这里访问Sammy的背景?在sammy之外为了得到参数?像Sammy.Application.context.params['term']但是课程申请没有这样的方法,所以不知道!? :(

我该怎么完全走错路了呢?我怎么能轻易通过查询字符串PARAMS我的模板内的访问对象,以便淘汰赛可以使用它。

你的帮助是极大的赞赏。

回答

0
<div id="contacts" class="view animated fadeInLeft"> 
    <h3>Search results for {{#routeData}}{{term}}{{/routeData}}</h3> 
    <ul data-bind="template: { name: 'searchresults-template', foreach: searchResults }"></ul> 
</div> 
<script type="text/html" id="searchresults-template"> 
    <li data-bind="text: type"></li> 
</script> 

<script> 

    require(['searchresults'], function (searchresults) { 
     var searchTerm = "{{#routeData}}{{term}}{{/routeData}}"; 
     searchresults.get(searchTerm); 
    }); 
</script> 
+1

我希望Sammy.JS网站能够在它的网站上推出的模板插件上写出更多段落。 – XGreen