This is a migrated thread and some comments may be shown as answers.

[Solved] WebAssets - Add to default group from config

2 Answers 140 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jay
Top achievements
Rank 1
Jay asked on 05 May 2010, 02:02 PM
I have a shared group called Common.  Which I was including in my site master.  And then my views would add shared groups as necessary.   This didn't work out though, the common group would actually render after the views shared groups.   I thought then that I would rename the Common group to default, and see if it would append to the default group.   But that doesn't work either.   Is there any solution for fixing the rendering of the script tags so that I can force certain things to show up before others.   jquery.ui for example.

Thanks,
-Jeff

2 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 05 May 2010, 02:34 PM
Hi Jeff Sheldon,

Currently you cannot add items to the default group. This however sounds like a reasonable feature to have and I have logged it for further consideration.

The AddSharedGroup method appends the scripts. By introducing a new method InsertSharedGroup(int position, string name) you can achieve your goal:

Here is how that method should look like (you can add the code in WebAssetItemCollectionBuilder.cs)
public virtual WebAssetItemCollectionBuilder InsertSharedGroup(int position, string name)
{
    Guard.IsNotNullOrEmpty(name, "name");
 
    WebAssetItemGroup group = (assetType == WebAssetType.StyleSheet) ?
                              SharedWebAssets.FindStyleSheetGroup(name) :
                              SharedWebAssets.FindScriptGroup(name);
 
    if (group == null)
    {
        throw new ArgumentException(TextResource.GroupWithSpecifiedNameDoesNotExistInAssetTypeOfSharedWebAssets.FormatWith(name, assetType), "name");
    }
 
    if (assets.FindGroupByName(name) == null)
    {
        // People might have the same group reference in multiple place.
        // So we will skip it once it is added.
 
        // throw new ArgumentException(TextResource.LocalGroupWithSpecifiedNameAlreadyExists.FormatWith(name));
 
        // Add a copy of the shared asset
        WebAssetItemGroup localGroup = new WebAssetItemGroup(group.Name, true)
        {
            DefaultPath = group.DefaultPath,
            UseTelerikContentDeliveryNetwork = group.UseTelerikContentDeliveryNetwork,
            ContentDeliveryNetworkUrl = group.ContentDeliveryNetworkUrl,
            Enabled = group.Enabled,
            Version = group.Version,
            Compress = group.Compress,
            CacheDurationInDays = group.CacheDurationInDays,
            Combined = group.Combined
        };
 
        foreach (WebAssetItem item in group.Items)
        {
            localGroup.Items.Add(new WebAssetItem(item.Source));
        }
 
        assets.Insert(position, localGroup);
    }
 
    return this;
}


Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jay
Top achievements
Rank 1
answered on 05 May 2010, 02:47 PM
Hmm.   I kind of like that snippet.   Makes what I did look a little bit like a hack.   I really like the ability to control my includes via config though, so the change I made, was to adjust the "CopyFrameworkScriptFiles" method in ScriptRegistrar to look like so:



        private void CopyFrameworkScriptFiles() 
        { 
            if (!ExcludeFrameworkScripts) 
            { 
                var defaultGroup = SharedWebAssets.FindScriptGroup("default"); 
 
                if (defaultGroup != null
                { 
                    defaultGroup.Items.Each(configAsset => DefaultGroup.Items.Insert(0, new WebAssetItem(configAsset.Source))); 
                } 
 
                FrameworkScriptFileNames.Reverse().Each(source => DefaultGroup.Items.Insert(0, new WebAssetItem(PathHelper.CombinePath(DefaultGroup.DefaultPath, source)))); 
            } 
        } 

Which will then append anything I've added in the config section like so:

<telerik> 
        <webAssets> 
            <scripts> 
                <add name="default" combined="false" compress="false" enabled="true"
                    <items> 
                        <add source="jquery-ui-1.8.custom.min.js" /> 
                        <add source="Common.js" /> 
                    </items> 
                </add> 
            <styleSheets> 
                <add name="CommonCss" combined="false" compress="false" enabled="true"
                    <items> 
                        <add source="Site.css" /> 
                        <add source="jquery-ui-1.8.custom.css" /> 
                    </items> 
                </add> 
            </styleSheets> 
        </webAssets> 
    </telerik> 

Which then has my scripts popping up right underneath the jQuery include and before any other SharedGroups.
Tags
General Discussions
Asked by
Jay
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Jay
Top achievements
Rank 1
Share this question
or