rendrjs / rendr

Render your Backbone.js apps on the client and the server, using Node.js.
MIT License
4.09k stars 312 forks source link

Dynamic Views (client-side) and attach/fetchLazy #367

Closed crwang closed 10 years ago

crwang commented 10 years ago

I've been trying to figure out how to do dynamic views while still retaining the way the collections are pulling data, etc. Normally, in backbone, I'd just do a collection.fetch and on('reset',this.render), but that doesn't seem to work.

I'm needing to load a popover with a detail list when I click on an item. I've figured out a way to do it, but I had to edit some of the core base View code and I wanted to see if I could get some ideas on why some things are as they are.

1) In fetchLazy in shared/base/view.js, what is the point of this line?

params[this.options.param_name] = this.options.param_value;

There doesn't seem to be a way to pass in params for fetchLazy. Is there a way to do this? I edited it to pass in params like this:

fetchLazy: function(params) {
  var  fetchSpec;
  if (params == null) {
    params = {};
  }
  params[this.options.param_name] = this.options.param_value;

The original code is this:

fetchLazy: function() {
  var  fetchSpec;
  params[this.options.param_name] = this.options.param_value;

2) Is view.js's attach method meant to be callable from outside code?

At least to get this to work, I added in:

    if (this.options.params) {
      this.fetchLazy(this.options.params);
    } else {
      this.fetchLazy();
    }

so that I could call it from the outside and pass in params.

Then, I'm able to load the view by doing this:

    var params = {};
    params.access_token = sessionData.access_token;
    params.masterItem_id = 2;
    var DetailListView = BaseView.getView('details/list');
    var view = new DetailListView({
        app: this.app,
        lazy: true,
        params: params,
        collection_name: ‘DetailsCollectionName'
    });
    view.attach('div.test', this);

Is there a better or easier way? If not, then, can you comment on this approach and if it's ok, I can submit a pull request?

crwang commented 10 years ago

I gave up on this since there was no feedback. Closing it. I went to pulling the data with this.app.fetch and then passed the data into a new view's collection instead since that works without any changes.