locked
refreshing observable model RRS feed

  • Question

  • Hi

    I am very new to creating metro store apps using html and javascript, and i am trying to fix up someone else's app.

    I have a selection box whereby the values are retrieved from the database, I want to be able to add values to that list, and the current selection box should be updated with the new value.

    I need to know how to "refresh" the model object?, or even retrieve the list again from the database.


    WinJS.Namespace.define("DataModel", { model: WinJS.Binding.as({ filtersListModel: {}

    }) });

    I tried using the processAll method after saving is done via ajax, but the filtersListModel still returns the original list.
    I also tried using the notify method, but that too didnt cause the filtersListModel to retrieve the new list.

    Thank you

    Sasha

    Wednesday, September 25, 2013 3:09 PM

Answers

  • Hi Sasha,

    I read the JQuery document for ajax(), there are some words I think may solve your problem:

    By default, requests are always issued, but the browser may serve results out of its cache. To disallow use of the cached results, set cache to false. To cause the request to report failure if the asset has not been modified since the last request, set ifModified to true.

    You could try to set cache as false to see if the ajax has been resend.

    $.ajax({
      url: "test.html",
      cache: false
    })

    Hope this time can solve your problem.

    Best regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, September 27, 2013 7:28 AM
    Moderator

All replies

  • Hi Sasha,

    Normally when WinJS.Binding.as() is used, the content will always change when the binding source changes, you don't need any processAll or notify method, I think the problem should be your filterListModel did not update.

    The code you provided looks correct, but is helped less with not update issue, could you give more code snippet to help with a further analysis? How do you fetch the list while the project is initializing and what is the code you used to update the filterListModel?

    Best Regrads,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, September 26, 2013 6:33 AM
    Moderator
  • Hi James

    I think you are correct James the filterModel did not update.

    I am not sure that there is code that updates the filterModel, I might have to add it, but not sure how to go about doing that either,  new to MVC as well :)

    the filterlistmodel is declared like this, and the method that gets the information from the database that I want is in the favorites method

    public class FiltersListModel {
    
    public FiltersListModel() { }
    
    public List<FavoriteViewModel> Favorites {
                get {
                    using (FavoritesRepository _repository = new FavoritesRepository()) {
    
                        List<FavoriteViewModel> _viewModels = _repository.GetByUser(HttpContext.Current.User.Identity.Name).Select(Fav => new FavoriteViewModel() {
                            FavoriteId = Fav.FavoriteId,
                            FavoriteName = Fav.FilterName,
                            FavoriteXml = Fav.XmlFilter
                        }).ToList();
    
                        _viewModels.Insert(0, new FavoriteViewModel() {
                            FavoriteId = 0,
                            FavoriteName = "SELECT",
                            FavoriteXml = string.Empty
                        });
    
                        return _viewModels;
                    }
                }
            }
    
    }

    I hope you can set me in the right direction to get this to work.

    Thank you

    Kind regards,

    Sasha

    Thursday, September 26, 2013 8:23 AM
  • Hi Sasha,

    The code looks correct too, can you provide me the code from FavoritesRepository class?

    You have the code below, which is the core method to fetch the latest list.

     _repository.GetByUser(HttpContext.Current.User.Identity.Name)

    I guess the code may contain xhr function, let's see what's happening inside. :)

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, September 26, 2013 8:45 AM
    Moderator
  • Hi James

     public class FavoritesRepository : RepositoryBase<FilterFavorite> {
    
            public IEnumerable<FilterFavorite> GetByUser(string UserName) {
                return this.EntityContext.FilterFavorite_Get(UserName);
            }
    
            public void Add(string UserName, string FilterName, string FilterXml) {
    
                this.EntityContext.FilterFavorite_Add(UserName, FilterName, FilterXml);
            }
        }

    Kind regards,

    Sasha


    Thursday, September 26, 2013 8:55 AM
  • Hi Sasha,

    "FilterFavorite_Get(String name)" is only a method, Windows Store app do not have such API, this method is a kind of self definition stugg, can you show us what's inside?

            public IEnumerable<FilterFavorite> GetByUser(string UserName) {
                return this.EntityContext.FilterFavorite_Get(UserName);
            }

    Now the code looks like C# but not JavaScript, is that means your project use a C# library or something?

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, September 26, 2013 9:33 AM
    Moderator
  • Hi James

    yes, the code that actually gets the data from the database is written in c#, using the mvc model, 

     public ObjectResult<FilterFavorite> FilterFavorite_Get(global::System.String userName)
            {
                ObjectParameter userNameParameter;
                if (userName != null)
                {
                    userNameParameter = new ObjectParameter("UserName", userName);
                }
                else
                {
                    userNameParameter = new ObjectParameter("UserName", typeof(global::System.String));
                }
        
                return base.ExecuteFunction<FilterFavorite>("FilterFavorite_Get", userNameParameter);
            }

    I know i should somehow get the data again from the db, but how to do that has me stumped.

    Kind regards,

    Sasha

    Thursday, September 26, 2013 9:46 AM
  • Hi Sasha,

    Oh, I just recognized that maybe I lead you to a wrong direction.

    Let's say you have two steps to update the data: 1, you have ajax to fetch the data from MVC for binding the Model to your interface, 2, MVC has the communication with DB. And now it seems the second step is correct from your code.

    I'm wondering if you can validate the following things?

    1, try to know if the code "return _viewModels;"  has been executed or not.

    2, can you tell me how you implement the ajax function?I think the problem maybe occurs here. Normally in WinJS we use  xhr function (I mentioned before) to make it work, you could read How to ensure that WinJS.xhr resends requests. Or if you are using a different way to send ajax request share with me please.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, September 26, 2013 10:29 AM
    Moderator
  • Hi James

    the code "return _viewModels" gets run right at the beginning of the app.

    so i am adding a new item to the list, and that gets saved to the db using this ajax code:

    SaveFavorite: function (data, name, chain, callBackFunc) {
                    var senddata = { "ViewModel": data, "FavoriteName": name, "Chain": chain };
                    $.ajax({
                        type: "POST",
                        contentType: "application/json",
                        url: UrlHelper.urls.urlLeftPart + UrlHelper.urls.saveFavorite,
                        error: function (jqXHR, textStatus, errorThrown) {
                            //TJ-TODO - Add error logic
                        },
                        success: function (data, textStatus, jqXHR) {
                            //TJ-TO DO Do something interesting
                            var t = textStatus;
                            var d = data;
                            var j = jqXHR;
                            
                        },
                        data: JSON.stringify(senddata),
                        dataType: "text"
                    });

    in the success portion i wanted to get the latest data from the db, then call another function to redraw the select box.

    Kind regards,

    Sasha

    Thursday, September 26, 2013 10:59 AM
  • Hi Sasha,

    It's really strange, ok, to ensure where the problem is we should track the following data.

    1, while adding the item to list, track if ajax send the correct data to db. (the problem be with ajax)

    2, check db if already updates with a latest data.(the problem with MVC saving data)

    3, validate if the data you fetch from db is correct. (the problem with fetching data)

    4, while showing the data to the select box, validate id the data source is correct (the problem with displaying data)

    Could you tell which part of the data is not correct?


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 27, 2013 2:15 AM
    Moderator
  • Hi James

    It is definitely number 3, it seems after the update is done in the db, when i call the observable object from the data binding, then it still has the "old" data, i want to tell, it to "wakeup" you got new data, but I don't know how to do that.

    Kind regards,

    Sasha

    Friday, September 27, 2013 6:34 AM
  • Hi Sasha,

    I read the JQuery document for ajax(), there are some words I think may solve your problem:

    By default, requests are always issued, but the browser may serve results out of its cache. To disallow use of the cached results, set cache to false. To cause the request to report failure if the asset has not been modified since the last request, set ifModified to true.

    You could try to set cache as false to see if the ajax has been resend.

    $.ajax({
      url: "test.html",
      cache: false
    })

    Hope this time can solve your problem.

    Best regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, September 27, 2013 7:28 AM
    Moderator
  • Hi James

    thank you for all your help.

    the caching seemed to have been the problem.

    Thanks again.

    Kind regards,

    Sasha

    Monday, September 30, 2013 8:37 AM