Using wymeditor with angularJS, routing and views

51 views Asked by At

I am using the WYMeditor for a project of mine, here is the api: http://wymeditor.readthedocs.org/

So i have detected my issue and have a solution, but my solution does not seem like the right approach.

I have multiple views defined with $routeProvider, they all work great, here is the issue:

I have an angularjs controller handling the view where the editor is, i initialize the editor in the controller like thus:

$scope.resume = GetModel();
$("#editor_summary").wymeditor({
html: $scope.resume.summary;
});

GetModel is a method that retrieves an object saved in localStorage, It loads the editor with html inside. and this works very well. now since this is the only time i use the editor in the project i have this code to retrieve the instance of the editor:

var summary_wym = jQuery.wymeditors(0);
//jQuery.getWymeditorByTextarea(jQuery('#editor_summary'));
//Tried also the getWymeditorByTextarea, yields the same results.

This works well the first time the app runs, but if i move to another view and then return, jQuery.wymeditors(0) is no longer the editor i need, the editor instance i need is jQuery.wymeditors(1), because the controller was called again after returning to the view, creating another instance of the editor which is more recent.

In this function i Update the localstorage with the editor data:

$scope.savechanges = function () {
    console.log(summary_wym.html());
    var resume = GetModel();
    resume.summary = summary_wym.html();
    UpdateModel(resume);
};

the function won't work properly the second time i load the page because the instance of the editor referenced here isn't correct.

A fix for this is creating a global variable in the parent controller and increasing it each time the child controller is called and calling the editor like thus:

jQuery.wymeditors(i);

When i is an incremented value each time the controller is called. This works and i checked it. But it seems to me there might be a better approach.

0

There are 0 answers