Great article in having multiple views within one page
http://blog.yojimbocorp.com/2012/02/14/multiple-view-models-with-knockout/
Javascript code
Great article in having multiple views within one page
http://blog.yojimbocorp.com/2012/02/14/multiple-view-models-with-knockout/
To me jQuery is javascript library that helps the developer in interacting with HTML element/DOM but what library that can be used to help in manipulating the Javascript object?I found “Underscore.js” which can be found in here (http://underscorejs.org). It provides the user to manipulate the JS object (e.g It provides function to manipulate Arrays, Add delay to your function, throttling your JS function, Check the data type of an object, etc)
A couple functions that I found useful
1. Manipulating your JSON object with “Where, filter and findWhere”
It’s pretty clean to set, bind and use enum in your javascript
//Enum declaration
var AvailableActions = { “Action”: 1, “Priority”: 2, “Status”: 3 };
//Bind
function Load()
{
for (var action in AvailableActions) {
jQuery(“#MandatoryActionList”).append(“<option value=” + AvailableActions[action] + “>” + action + “</option>”);
}
}
//Use the enum
AvailableActions[“Priority”] will return 2
action will return the string representation in this case e.g “Action”, “Priority”, or “Status”
This is the javascript coding standard that my colleague found on Github, but seriously this is a good standard and I recommend it to any developer
By using this library, it allows you to do the paging through the HTML DOM from the client side (Note: this is not about the ideal way or not the ideal way, I know the ideal way is to do paging server side)
To implement you just need to do 3 things:
1. Create a div container that wraps the container of item that you want to repeat and the navigation div, you can call it whatever you want
2. Create a div inside the container with class “page_navigation”
3. put class “content” on the container of the list item
Sample
and I put this code on the document.ready event based on the id set on item 1
The source code can be downloaded from here
and you can read the documentation from this github page
Knockout allows you to bind the HTML to your javascript object. It simplifies DOM manipulation and allow the portability of the javascript object and action. It is pretty much the same concept as MVVM in silverlight. You can wire up the function with button click easily, you can have for each against your array (e.g like repeater). It is so elegant, but debugging sometimes can be challenging as well. I’ve used Knockout along with JSON that allows me to build rich and interactive website
2 powerful function: ko.observable – this allow knockout to monitor this object value, ko.observableArray this is the extension of ko.observable against the array. With observable, knockout will keep tracking the value of that property and allow the DOM that has been bind against it to refresh
You can bind initial data from your MVC model to the variable in javascript and bind it, in this sample below, I use ToJson extension function
Sample code
Source:
Initially, I always pass individual object properties through JSON and form the model in the controller as I wasn’t sure how to pass/form a model from Javascript to controller
Controller
Javascript
the code above is working just fine but I still feel that there is room for improvement. Below is the code that shows how you can still have your controller accepting the model instead of expanding the properties in the model as the parameters to the controller. The solution is just to have the model being assigned to a variable (in this context called as data) before passing it to the JSON, my previous code was forming the actual object in the JSON code
Controller
Javascript
I’ve been learning Knockout recently and I found that sometimes it is hard to find out how your View Model is being formed especially when you create the view from Model in MVC
To inspect your view model, you can use firebug console and run below command, this will give you a full navigated View Model and allow you to drill down
console.group(“Dumping view model”);
console.dir(ko.toJS(new JobResultViewModel()));
console.groupEnd();
alternatively you can also put this code in your template
<div data-bind=”text: ko.toJSON($data)”></div>
Feel free to let me know if you have any other way to debug
I was struggling in finding out of how to make a simple delegate like in AJAX request through javascript. It takes me a few days to figure this out
Basically the problems are:
-I need to execute a piece code of javascript after getting the ticket from web service function
-The webservice function might not be responding to the first request because it waits for the state of the other external component
-The webservice will give the response when the external component is ready
-The client does not know when the external component is ready, neither the web service. But it wil be ready within 1-5 minutes which again depending external component
Possible Solution:
-Using the setTimeOut(function(){acquireTicket();}, 300000) will cause the application to wait for 5 mins before calling the web service , this approach will slowing down the user experience and waste of time because the external component can be ready earlier than 5 mins
-Using the while loop is not good because it makes the browser freezing while waiting and it will wasting the processing power because of the looping
Recommended Solution:
-Recall the function by itself using setTimeout Function using parameter to indicate whether it should go out of the loop or not
-The web service will be checked for every 2 seconds to check the response from the external component. Once the external component is ready then it will move on to execute the next line of code
ParseJSON returning you an object from your AJAX Call, the problem that I have is my object properties has invalid character (e.g “#”)
Assuming jsonData is my variable that contains the following information
I’d like to grab the property of “#innerxml”
Normally I can do this to get the property of an object but in this case I can’t due to invalid character
(jQuery.parseJSON(jsonData)[0]).#innerxml
So How do I access an object which has properties where one of the property name is using an invalid character (e.g “#”)
I can access with the following style
(jQuery.parseJSON(jsonData)[0])[‘#innerxml’]
Powered by WordPress & Theme by Anders Norén