Thursday, May 17, 2012

Telerik MVC 3 Controls Injected Dynamically via Template

So... I want to use the Telerik MVC controls (numeric textbox, datepicker, etc) in a template, but rendering the template breaks Telerik's code. Also, I don't want to use an ID to identify them, since I'm using them for each child item. What's a guy to do?

Well, I looked at the response from my partial view, and found that Telerik is initializing them each in their own javascript in the partial view.

So, I pulled out that logic and did it myself.


            $("input[name=editorDate]", control).tDatePicker({
                format: 'M/d/yyyy',
                minValue: $this._model.startDate,
                maxValue: $this._model.endDate
            });
 
            $("[name=percentValue]", control).tTextBox({
                val: 0,
                step: '1',
                minValue: 0,
                maxValue: 100,
                digits: 2,
                groupSize: 3,
                positive: 0,
                negative: 0,
                text: 'Enter value',
                type: 'percent'
            });
            $("[name=amountOffValue],[name=priceValue]", control).tTextBox({
                val: 0,
                step: '1',
                minValue: 0,
                maxValue: 1000000,
                digits: 2,
                groupSize: 3,
                positive: 0,
                negative: 0,
                text: 'Enter value',
                type: 'currency'
            });

 I call these functions after I instantiate a template, and it works great! There are some .css issues with the z index of Telerik's controls, but some finely applied css classes fixed that. 

I didn't see anything on the internet that solved this issue, so I posted this fix. Hope this helps someone!



F~

Tuesday, May 15, 2012

The difference between a good source control system and TFS 2010

So... We've switched to TFS from SVN. I'd heard good things about it, and was looking forward to using it. Years ago I'd used TFS and I really didn't have much of an issue. I liked the shelf set feature. It made it very easy to put aside changes I'd made to work on a different task, great for urgent bug fixes.

The company I work for implemented a new policy. Gated commits. I'd done automated code reviews when using Hg and FogBugz, and that was wonderful, so I was looking forward to TFS' implementation. I couldn't have been more wrong.

With gated checkins, you create a shelf set. Then you create a "code review" task. A part of the code review task creation process is to manually link your shelf set to the task. This consists of pasting the string name of the shelf set into the text field "shelf set name". Seriously. If you forgot to copy the name when you created the shelf set, go back and open its details to get its name.

Then you assign it to the reviewer and wait.

Or, continue working.

If you continue working, you must shelve your new changes to pull back your old changes. None of this is actually enforced. You just can't commit unless you have an approved code review task. So, you shelve your new changes, unshelve the approved set, get latest, test the code, and commit.

Now you see lots of changes that aren't even changes. Right-click the files in your Pending Changes window, and many of them show that they're identical. TFS can't tell when a file is identical. Wastes time, since I like to review my changes from time to time. Make sure I didn't change anything I didn't mean to change.

Here's a worst case scenario.

Say, a dev is working on code. They need you to take over. So, you shelve the work you're working on, take their shelf set, apply it. Fix it, then try and merge your previous shelf set.

Yeah, you're pretty much screwed.

It won't let you.

You do what I did, and search Google and find
http://blogs.infosupport.com/the-how-and-why-behind-tf203015-lt-file-gt-has-an-incompatible-change-while-unshelving-a-shelve-set/

Yep. You need a special tool to merge shelfsets when your working copy has changes. Every other (decent) source control has ways of doing this. So, you need to get a VS power tool, and hit the VS command line to do it in TFS. Hot, huh...

Even still, it only brings over your "merges" to your working copy. Your shelfset will always and forever be unuseable.

Shoulda stuck with FogBugz...

Monday, May 14, 2012

Posting a JSON object graph to MVC


Passing an entire json object graph via jQuery to an MVC action method.

This covers string arrays, int arrays, dates, etc. MVC is actually pretty good at this. You just can't use jQuery's post method, as it sends the JSON object as a form post.


<script language="javascript" type="text/javascript">
    var testModel = {
                    NumberList: [100, 200, 300, 400 ],
                    NumberArray: [101, 102, 103],
                    StringArray: ["a""b""c""d"],
                    Date: new Date(),
                    Boolean: true
                };
    $(function () {
        $("#submit").click(function () {
            var model = {
                Id: 123,
                Name: "foo",
                ProductIds: [1, 2, 3, 4, 5],
                StoreIds: [11, 12, 13, 14],
                TestModel: testModel
            };
 
            $.ajax({
                url: "/Test/SaveInvoice",
                data: JSON.stringify(model),
                success: function (data, xhr) {
                    alert(data);
                },
                dataType: "json",
                contentType: "application/json",
                type: "post"
            });
        });
    });
</script>