Hacker News new | ask | show | jobs
by BillGoates 4896 days ago
Separating DOM and data I can understand, but I am not convinced Backbone is the right way to do it.

My code would look more like:

    function postdata(url, data, callback) {
        $.ajax({
            url: '/status',
            type: 'POST',
            dataType: 'json',
            data: data,
            success: function (received) { callback(received); }
        });
    }


    var statusform;

    (function(pub) {
        var status = ""

        pub.init = init;
        function init() {
            $('#new-status form').submit(function(e) {
                submitdata(); 
                e.preventDefault(); 
            });
        }

        function submitdata() {
            var data = {
                text: getfieldvalue("textarea")
            };

            if (!data.text) { return; }
            postdata("/status", data, function(received) {
                status = received.status;
                drawscreen();
            });
        }

        function getfieldvalue(fieldname) {
            return $('#new-status').find(fieldname).val();
        }

        function drawscreen() {
            if (status) {
                $('#statuses').append('<li>' + status + '</li>');
                $('#new-status').find('textarea').val('');
                status = "";
            }
        }
    }) (statusform);


    $(document).onload(statusform.init);

Note 1: This code sample is untested, but should give a good enough idea what I am trying to do. Note 2: For extra type safety, use typescript interfaces to model data.
1 comments

Apologies for off-topic, but how does one post code, or do quoted text on HN? I've seen it done here and a few other places, but haven't been able to find documentation on how.
"Text after a blank line that is indented by two or more spaces is reproduced verbatim. (This is intended for code.)"

http://news.ycombinator.com/formatdoc