Picture of Nick Webb
Extension development - help with a scoping issue
by Nick Webb - Monday, 25 April 2016, 8:21 AM
 

Hi all,

I'm developing an extension at the moment that will extend the article model and view. I've been cribbing from the the assessment extension somewhat but I have a question regarding scope and the way the model and view are extended.

As far as I can see the assessmentModel extends the articleModel prototype and so will apply to all articleModel instances.

The assessmentView is only applied to articles where the extension has been enabled, so the assessmentView is only applied the the article instance, leaving the artlceView prototype unchanged. So the postRender method defined in assessmentView is an instance method.

Have I got that right? If so presumably if I write my extension to use a postRender method then I couldn't use my extension and the assessment extension on the same article as they would both try to overwrite the article instances postRender method?

 

Thanks for any help,

Nick

Picture of Oliver Foster
Re: Extension development - help with a scoping issue
by Oliver Foster - Monday, 25 April 2016, 6:04 PM
 

Hi Nick,

 

You've got that almost exactly right. I think both the model and the view only apply their extensions when the article has an enabled assessment.

Each article triggers a Backbone event "articleView:postRender" you can listen to.

The assessmentView doesnt entirely replace the articleView postRender, more encases it to extend the behaviour. In theory, if you replaced the articleView.prototype.postRender function with your own version, assessmentView would carry on as normal but use your function in place of the original.

The current implementation is horrid but for the moment it works. We have no formalised pattern for extending views at runtime, so it leaves the block, article and page views a little behaviourally deficient. It might suit you better to have a secondary view, with the same model complimenting the article rather than extending it? 

Do let me know how you get on? Perhaps you could suggest a better way to implement the article assessment behaviour having gotten this far?

Ollie.

Picture of Nick Webb
Re: Extension development - help with a scoping issue
by Nick Webb - Tuesday, 26 April 2016, 7:41 AM
 

Hi Ollie,

Thanks for clarifying.

Regarding the postRender method. If I've understood everything correctly, I think the main problem with the way the assessment extension defines the postRender method is that if I do the same thing with my extension and I try to apply both extensions to the same article, only one of those extensions will work properly wont they?

I think being event driven would be a preferable approach (which is what I've tried to do). This would then allow extension methods to be namespaced to avoid conflicts.

I think the problem with the articleView:postRender event at the moment is that it's triggered on the Adapt object with the target article passed as an event parameter. If I listen to this event but have three articles on the same page, then my postRender method will be called three times and I will need to check the target each time. Would it not be better to have the articleView:postRender (and associated events) triggered on the article itself, as well as on the Adapt object?

Would appreciated your thoughts.

Kind regards,

Nick

Picture of Oliver Foster
Re: Extension development - help with a scoping issue
by Oliver Foster - Thursday, 28 April 2016, 7:47 AM
 

Yup, I agree that having the view trigger it's own events would be useful.