As for your other query about what to replace the 'var viewModel = new
MvcApp.Restaurant( ko.toJS(serverData) ); ' with, that's where you
need to get a bit more creative.
If MvcApp.Restaurant is a javascript object that can take the initial
data, then you don't actually need to change it for anything. In
otherwords, in my code, I do this...
// attempt to bind any data we received from the server
var serverData = @(new MvcHtmlString(Model.ToJson()));
// pass any data we received into a new viewModel
var viewModel = new Lightcast.Aspect( ko.toJS(serverData) );
Lightcast.Aspect is an object of its own to Javascript, and it accepts
a single variable. This variable is assumed to be a Javascript object
with its own properties.
If there is no data passed into my View, then 'serverData' is just
"{}", and it is basically an empty Javascript object. So calling 'new
Lightcast.Aspect({})' is the same thing. Each field tries to populate,
and finds nothing to be populated with, so it defaults to an empty
observable or observableArray(). This way, this same code works for
both Display views and Input Views.
So the truth is you don't really need to change that line!
On that note, let me show you what I am working on. You might like it.
http://pastie.org/2037470
This is a replacement of my "Sample View" from my original pastie
using some HtmlTag code I wrote inspired by FubuMVC (It is not
FubuMVC, I just took his code and I _very literally copy and pasted a
lot of it_. I do not, I repeat I do not take any kind of credit for
the idea or even most of the implementation. I just re-wrote it to
suit my specific tastes and to fit the MVC Views)
Notice how I define my Html Tags with a fluent syntax.
Html.EnumTagFor( model => model.Classification, tag => {
tag.Name("Classification")
.Id("Classification")
.Bind("value", "Classification")
.Validate();
})
I am still using the Html Helpers. I am still getting the Html5
Validation Syntax in the output HTML, but now I can very specifically
fine-tune them without that long, and rather ambiguous
IDictionary<string, object> going on. It's kind of funny that I would
wand more code over less, but sometimes more clean code is better than
less dirty code -- if that makes any sense. I've included a property
called "Bind". This allows you to define it an infinite number of
times, and it cascades all of them into a single data-bind="property:
value, property2: value2, etc: etcValue" attribute on the Html (thus
the different bindings that Knockout supports).
You can see an example of that on the Text Box.
@Html.TagFor( model => model.Name, tag => {
tag.Is("input")
.Type("text")
.Name("Name")
.Id("Name")
.Attribute("size", "45")
.Bind("value", "Name")
.Bind("valueUpdate", "'change, blur'")
.Validate();
})
With a fluent syntax, I just tell it how I want my HTML to look.
However then you'll be confused by the Markdown Editor. This uses
Markdown Deep (
http://www.toptensoftware.com/markdowndeep/) to render
a Markdown Editor to the screen similar to what you see on
Stackoverflow. I am especially proud of this one..
@Html.HtmlEditorFor( model => model.Description , editor => {
editor
.Id("Description")
.Name("Description")
.Attribute("cols", "50")
.Attribute("rows", "10")
.Bind("value", "Description")
.Validate();
}, "__preview_aspect")
The Extension Method that defines this looks like this...
http://pastie.org/2037489
Notice how I am almost writing the same syntax in my helper as I would
in my View, but it is still fluent like I am doing Fluent nHibernate
or Entity Framework CodeFirst.
htmlEditor
.Class("mdd_editor")
.Attribute("data-mdd-preview", String.Format("#{0}", preview));
htmlEditor.Before(htmlToolbar => {
htmlToolbar.Is("div").Class("mdd_toolbar");
})
.After(htmlResizer => {
htmlResizer
.Is("div")
.Class("mdd_resizer")
.After(htmlPreview => {
htmlPreview.Is("div").Id(preview);
});
});
It draws out tag Hierarchy using TagBuilder (I went with TagBuilder
instead of HtmlTextWriter. Still debating on the two. I had a lot of
trouble with Opening/Closing Tag issues using HtmlTextWriter, whereas
TagBuilder seemed 'smart' and could figure out self closing tags on
its own).
So this will actually draw out the syntax
<div class="mdd_toolbar"></div>
<textarea class="mdd_editor" data-mdd-preview="#__preview_aspect"
rows="10" cols="50" ></textarea>
<div class="mdd_resizer"></div>
<div id="__preview_aspect"></div>
Which can seamlessly plugin to Markdown Deep.
Then the last part of the first pastie, my Template Helper.
<div class="clearfix right">
<button data-bind="click: open">New Property</button>
<button type="submit">Submit</button>
</div>
<div data-bind='template: { name: "__property_template", foreach:
Properties }'></div>
@using(Html.Template("__property_template")){
<text>${ Id }</text>
}
This one does exactly what it looks like it does. It writes out ...
<script id="__property_template" type="text/html">
{any content I want goes in here}
</script>
And I am looking at writing another Tag Building Helper for actually
creating the <div data-bind='template ... section where it would look
like ..
@Html.Binding( template => {
template.Name("__property_template")
.ForEach("Properties")
.FromViewModel("viewModel")
.Is("div")
})
But I'm not sure yet. Let me know if you think this would be at all
useful to you, I would be happy to paste my entire source code up so
you can use it. The HtmlTags actually work and I am using them right
now in a real world project.
On Jun 8, 7:47 am, Stacey Thornton <
stacey.cielia.l...@gmail.com>
wrote: