knockout sammy.js navigation issue

元气小坏坏 提交于 2019-12-13 18:20:12

问题


I have a scenario like this.

  1. Initially loaded when page is navigated to #Action.
  2. Once the select action is performed data-bind="with tag is loaded"
  3. User click on "Do Something" action is performed. Which replaces the whole "parentNode"
  4. Now When the user clicks back, and the sammy.js notices the hash tag #Action/:id, I need to load the #Action to get back the main View and then select the id to load the data-bind="with" tag again.

How can I do this?

Currently the page does go back to "Action/:id" but since main view is not loaded it doesn't do anything. I have used "this.redirect("#Action") then selected the node, but the doesn't work.

<div id="parentNode">
    <ul data-bind="foreach: items">
       <li data-bind="click: $root.selectItem">
          <h2><span data-bind="text: Sometext"></span></h2>
       </li>
    </ul>
    <div data-bind="with: selectedItem">
       <a data-bind="click: loadSomething">Do Something</a>
    </div>
</div>

In my viewmodel i have this:

viewModel.selectItem= function (item) {
    location.hash = "Action/" + item.id();
}

viewModel.loadSomething = function () {
    location.hash = "Action/" + viewModel.someSelectedItem().id() +"/SubAction";
}

$.sammy(function () {

  this.get('#Action', function () {
     $.ajax({
         url: '@Url.Action("GetMainView")',
         type: "GET",
         data: self.someId(),
         dataType: "json",
         success: function (result) {
           $("#parentNode").html(result.message);
         }
   });

  this.get('#Action/:id', function () {
      var id = this.params["id"];
      var matchItem = ko.utils.arrayFirst(viewModel.MainItems(), function (item) {
                return item.id() == id;
       });

       viewModel.someSelectedItem(matchItem);
  });

  this.get('#Action/:id/SubAction', function () {
    var id = this.params['id'];
    $.ajax({
         url: '@Url.Action("ViewSomething")',
         type: "GET",
         data: { id: id },
         success: function (result) {
             $('#parentNode').html(result.message);
         }
    });
 });
});

Sample Code: https://skydrive.live.com/redir?resid=33048714B5BF3B4B!913

Steps to Reproduce:

  1. Select "SubItems" under any of the items listed (Item 1, Item 2, Item 3)
  2. Select any of the Sub Items that Label (Sub Item 1, Sub Item 2)
  3. Partial View will be shown with "Sub Item {x}" with "Next View" link
  4. Click "Next View" link.
  5. "Next Partial View" will be shown.
  6. Press the back button.

The thing I am trying to do is to load the SubItems and Select "Sub Item 1" view.

  1. List item

回答1:


I'm not sure this will work, but could you create a separate helper function to load the main view. Then it would be the case of the following:

this.get('#Action', function () {
    LoadMainView();
}

this.get('#Action/:id', function () {
    if($('#parentNode').length == 0) {
        LoadMainView(function() {
             var id = this.params["id"];
             var matchItem = ko.utils.arrayFirst(viewModel.MainItems(), function (item) {
                 return item.id() == id;
             });

             viewModel.someSelectedItem(matchItem);
        })
    }
}

Your LoadMainView function would then accept a callback and be something like this:

function LoadMainView(callback) {
    $.ajax({
        url: '@Url.Action("GetMainView")',
        type: "GET",
        data: self.someId(),
        dataType: "json",
        success: function (result) {
            $("#parentNode").html(result.message);
            if(typeof callback == "function") {
                callback();
            }
        }
    });
}

I haven't been able to test this in your solution (I get an error opening it), but I believe that's the general structure to do what you are asking.



来源:https://stackoverflow.com/questions/14765127/knockout-sammy-js-navigation-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!