Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
MVC, WebAPI Controllers and LocalStorage
Message
From
07/07/2014 12:49:02
 
 
To
07/07/2014 08:16:37
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 4.0
OS:
Windows 8
Network:
Novell 6.x
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01603183
Message ID:
01603292
Views:
58
>>>>>Thanks Viv.
>>>>>
>>>>>I have my data stored in local storage and now I want to display it. I've been using knockout like this:
>>>>>
>>>>>
this.runSheetDetails = ko.observableArray(@Html.Raw(Model.RunSheetDetailsJSON));
>>>>>
>>>>>So I've tried changing that like this:
>>>>>
>>>>>
this.runSheetDetails = ko.observableArray(JSON.parse(localStorage[this.localKey]));
>>>>>
>>>>>but then I get binding errors:
>>>>>
>>>>>Uncaught ReferenceError: Unable to process binding "template: function (){return { name:'list-template',foreach:runSheetDetails} }"
>>>>>
>>>>>Do you have an example of reading a list of data from localstorage and displaying it?
>>>>>
>>>>>This is an example of what the json looks like when it comes out of local storage:
>>>>>
>>>>>[[{"airWayBill":{"customer":{"cus_PK":"5df642b5-7889-419b-8a16-a63196f79571","cus_lastname":"Bachan","cus_firstname":"Kathy-Ann","cus_company":"Kath-Ann Bachan","cus_addr1":"23 Alexandra Street","cus_addr2":"C/O British Academy","cus_number":"6695 "},"awb_pk":"b9311877-eaf1-49b3-bfaf-2e8afedce389","awb_pieces":1,"awb_cusfk":"5df642b5-7889-419b-8a16-a63196f79571","awb_number":"99800640310"},"deliveryHistories":[],"runSheetHeader":null,"rnd_pk":"7c8d9f45-813b-4120-945d-91e4cfbaedba","rnd_rnhfk":"548a5288-c65d-43ac-be7c-fec662fc154a","rnd_awbfk":"b9311877-eaf1-49b3-bfaf-2e8afedce389"},{"airWayBill":{"customer":{"cus_PK":"4b0a83af-ef75-4567-b564-d4288ed193a0","cus_lastname":"Ambrose","cus_firstname":"Leonardo","cus_company":"Leonardo Ambrose","cus_addr1":"11 Braemar Rd,","cus_addr2":null,"cus_number":"1463 "},"awb_pk":"bea8497f-b956-4a4a-9702-c5eddcf014c7","awb_pieces":1,"awb_cusfk":"4b0a83af-ef75-4567-b564-d4288ed193a0","awb_number":"99802580636"},"deliveryHistories":[],"runSheetHeader":null,"rnd_pk":"39d225f2-b064-45fa-8034-7d6a92aa7b4e","rnd_rnhfk":"548a5288-c65d-43ac-be7c-fec662fc154a","rnd_awbfk":"bea8497f-b956-4a4a-9702-c5eddcf014c7"}"]
>>>>>
>>>>>(note I deleted a lot of the data so may not ave terminated the json properly)
>>>>
>>>>What code did you use to put the above *into* localStorage. Can you compare the output from the MVC Html.Raw() with the above object ?
>>>
>>>This is the code in my cshtml that saves the data into LocalStorage:
>>>
>>>
            var vm = new ViewModel();
>>>
>>>            var serialized = JSON.stringify(vm.runSheetDetails());
>>>
>>>            runsheetdetailsDataService.saveLocal(serialized);
>>>
>>>This is the saveLocal code:
>>>
>>>
            saveLocal: function (newrunsheetdetail) {
>>>                if (Modernizr.localstorage) {
>>>                    var
>>>                        ls = window.localStorage,
>>>                        key = ds.localKey;
>>>
>>>                    if (ls.getItem(key) == null) {
>>>                        ls.setItem(key, '[]');
>>>                    }
>>>
>>>                    var existing = ls.getItem(key);
>>>                    existing = ds.parse(existing);
>>>
>>>                    existing.push(newrunsheetdetail)
>>>
>>>                    var serialized = JSON.stringify(existing);
>>>                    ls.setItem(key, serialized);
>>>                }
>>>            },
>>>
>>>The data going in and coming back looks exactly the same.
>>
>>Only looked at this briefly but I'm a little confused. You are passing a stringified version of 'runSheetDetails' to your saveLocal method - which sounds as if there is more than one instance of runSheetDetail ?
>>
>>If that's the case then, in local Storage, you are creating an array of arrays. But when you pull it back in it seems you are expecting a single array ?
>
>Sorry for the delayed response.
>
>runSheetDetails contains a list of all the Air Way Bills/Packages that need to be delivered. I am saving this list to localstorage in one process and then wanting to read it back from localstorage and display it on a web page. The initial save saves the entire list in one long string under one key (which I guess may be an array of arrays). When I read it back I get back that exact same long string. I want to process through that string and display each Air Way Bill on the page.

But is the 'runSheetDetail' parameter of saveLocal just one AWB or a collection of them? You're retrieving the content of the key (which is presumably an array of AWB's then *pushing* the parameter content. This will append if to the array. If it's a single item then that's fine - but, if it is an array then you would need to push each element individually. Hope that makes sense....

But if you are sure the content of the key that you pass to ko.ObservableArray is *exactly* the same as that obtained from the database then I don't know what to suggest - Knockout is not my subject :-{
Previous
Reply
Map
View

Click here to load this message in the networking platform