jquery + codeigniter + knockout + sammy, cannot retrieve post data -
i'm using codeigniter 3.0rc2, jquery 1.11.3, knockout.js 3.3.0 , sammy.js. i'm trying post call , retrieve json data put in viewmodel.
my view is:
<!-- mails grid --> <table class="mails" data-bind="with: chosenfolderdata"> <thead><tr><th>from</th><th>to</th><th>subject</th><th>date</th></tr></thead> <tbody data-bind="foreach: mails"> <tr data-bind="click: $root.gotomail"> <td data-bind="text: from"></td> <td data-bind="text: to"></td> <td data-bind="text: subject"></td> <td data-bind="text: date"></td> </tr> </tbody> </table>
my viewmodel is:
function webmailviewmodel() { // data var self = this; self.folders = ['inbox', 'archive', 'sent', 'spam']; self.chosenfolderid = ko.observable(); self.chosenfolderdata = ko.observable(); self.chosenmaildata = ko.observable(); // behaviours self.gotofolder = function (folder) { location.hash = folder }; self.gotomail = function (mail) { location.hash = mail.folder + '/' + mail.id }; // client-side routes sammy(function () { this.get('#:folder', function () { self.chosenfolderid(this.params.folder); self.chosenmaildata(null); $.post("<?php echo base_url().'index.php/app/getmail'; ?>", { folder: this.params.folder }, self.chosenfolderdata) }); this.get('#:folder/:mailid', function () { self.chosenfolderid(this.params.folder); self.chosenfolderdata(null); $.post("<?php echo base_url().'index.php/app/getmail'; ?>", { mailid: this.params.mailid }, self.chosenmaildata); }); this.get('', function () { this.app.runroute('get', '#inbox') }); }).run(); };
at line of viewmodel:
$.post("<?php echo base_url().'index.php/app/getmail'; ?>", { folder : this.params.folder }, self.chosenfolderdata)
i know app/getmail send me correct data. tested with:
$.post("<?php echo base_url().'index.php/app/getmail'; ?>", { folder : this.params.folder }, function(data) { console.log(data) } )
and can see data retrieved @ console. if replace $.post data directly self.chosenfolderdata, that:
self.chosenfolderdata( { "id" : "inbox", "mails" : [{"id" : 1, "folder" : "inbox" ... }] );
it worked well. code is, return:
uncaught referenceerror: unable process binding "foreach: function (){return mails }" message: mails not defined
i realize data not assigned self.chosenfolderdata. tried that:
$.post("<?php echo base_url().'index.php/app/getmail'; ?>", { folder : this.params.folder }, function(data) { console.log(data); self.chosenfolderdata(data); } )
but not works. can see data @ console, have same error.
by way, based on knockoutjs.com tutorial here: webmail
can me?
i found answer. need put 'json'
@ end of $.post
call, this:
$.post("<?php echo base_url().'index.php/app/getmail'; ?>", { folder : this.params.folder }, self.chosenfolderdata, 'json');
Comments
Post a Comment