the_farmer the_farmer - 5 months ago 59
CSS Question

How to load a dynamic view css file using requireJs & Backbone

I need that each view will load it's own css file when navigate to the view.
currently the css file have been loaded ( I can see it loaded on the network section) but it's not effect the page style

any idea ?

define([
'jquery',
'text!../templates/Login.html',
'text!../css/styleshet.css'
], function($, html, css){

});

Answer

Apparently you need to add it to your page manually... the fixed is :

define([
    'jquery',
    'text!../templates/Login.html',
    'text!../css/styleshet.css'
], function($, html, css){      

//this will remove the old view css 
if ($("#customCss").length > 0)
            $("#customCss").empty();

//this will add the current css to the page 
 $('head').append('<style type="text/css" id="customCss"> ' + css + '</style>');

});

Thanks to Radek Michna

Comments