0

I have defined a class and am asking m.request to cast a web service's JSON response to it, but each of the class properties come out equal to n/b(), and my view renders each property as function (){return arguments.length&&(a=arguments[0]),a}.

If I do not attempt to auto-cast the JSON response to my class in m.request, then my view renders just fine, which I think tells me that the JSON object returned by the web service is valid JSON.

I want to use my class. What is wrong?

Here is an edited sample of the JSON returned by the web service:

{
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "q":"blah blah",
      "indent":"true",
      "wt":"json"}
  },
  "response":{
    "numFound":97,
    "start":0,
    "docs":[
      {
        "identifier":"abc123",
        "heading":"A Great Heading",
        "id":"abc-123-1",
        "title":"A Title",
        "url":"path/to/some.html",
        "content":["Blah blah blah blah blee blah."]
      },
      {
        "identifier":"xyz789",
        "heading":"Another Heading",
        "id":"xyz-789-1",
        "title":"Another Title",
        "url":"another/path/to.html",
        "content":["My bonny lies over the ocean."]
      }
    ]
  }
}

Here is my Mithril app:

var findus = {};

findus.Document = function (data) {
    this.id = m.prop(data.id);
    this.title = m.prop(data.title);
    this.heading = m.prop(data.heading);
    this.identifier = m.prop(data.identifer);
    this.url = m.prop("//" + data.url + "#" + data.identifier);
};

findus.vm = (function() {
    var vm = {};
    vm.init = function () {

        // user input
        vm.queryText = m.prop("");

        vm.search = function () {
            if (vm.queryText()) {
                vm.results = m.request({
                    method: "GET", 
                    url: "/prd/query?q=" + vm.queryText(), 
                    type: findus.Document,
                    unwrapSuccess: function (response) {
                        return response.response.docs;
                    },
                    unwrapError: function (response) {
                        console.log(response);
                    }
                }).bind(vm);
            }
        };


    };
    return vm;
}());

findus.controller = function () {
    findus.vm.init();
};

findus.view = function () {

    return [
        m("input", {onchange: m.withAttr("value", findus.vm.queryText), value: findus.vm.queryText()}),
        m("button", {onclick: findus.vm.search}, "Search"),
        findus.vm.results ? m("div", [
            findus.vm.results().map(function (result) {
                return m("div", [
                    m("h2", result.heading),
                    m("p", result.content),
                    m("a", {href: result.url}, result.url)
                ]);
            })
        ]) : ""
    ];

};

m.module(document.body, {controller: findus.controller, view: findus.view});
Peaeater
  • 626
  • 5
  • 19

1 Answers1

1

Oh, bugger. I forgot that my class properties are getter/setters via m.prop, so I should have been calling them as functions in the view -- see below.

False alarm, problem solved, I'm embarrassed.

findus.view = function () {

    return [
        m("input", {onchange: m.withAttr("value", findus.vm.queryText), value: findus.vm.queryText()}),
        m("button", {onclick: findus.vm.search}, "Search"),
        findus.vm.results ? m("div", [
            findus.vm.results().map(function (result) {
                return m("div", [
                    m("h2", result.heading()),
                    m("p", m.trust(result.content())),
                    m("a", {href: result.url()}, result.url())
                ]);
            })
        ]) : ""
    ];

};
Peaeater
  • 626
  • 5
  • 19