Skip to content

Commit

Permalink
Added an overload for RegisterActionlessViews that allows configuring…
Browse files Browse the repository at this point in the history
… the behavior chain depending on the view token
  • Loading branch information
uluhonolulu authored and jmarnold committed Nov 21, 2011
1 parent 48c9d37 commit 59d7aa3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/FubuMVC.Core/Registration/DSL/ViewExpression.cs
Expand Up @@ -50,6 +50,18 @@ public ViewExpression RegisterActionLessViews(Func<IViewToken, bool> viewTokenFi
return this;
}

/// <summary>
/// Specify which views should be treated as actionless views.
/// </summary>
/// <param name="viewTokenFilter"></param>
/// <param name="configureChain">Continuation for configuring each generated <see cref="BehaviorChain"/>, depending on the corresponding view token</param>
/// <returns></returns>
public ViewExpression RegisterActionLessViews(Func<IViewToken, bool> viewTokenFilter, Action<BehaviorChain, IViewToken> configureChain)
{
_viewAttacher.Apply(new ActionLessViewConvention(viewTokenFilter, configureChain));
return this;
}

/// <summary>
/// Fine-tune the view attachment instead of using <see cref="TryToAttachWithDefaultConventions"/>
/// </summary>
Expand Down Expand Up @@ -147,9 +159,15 @@ public ViewExpression ApplyConvention<TConvention>(TConvention convention)
public class ActionLessViewConvention : IViewBagConvention
{
private readonly Func<IViewToken, bool> _viewTokenFilter;
private readonly Action<BehaviorChain> _configureChain;
private readonly Action<BehaviorChain, IViewToken> _configureChain;

public ActionLessViewConvention(Func<IViewToken, bool> viewTokenFilter, Action<BehaviorChain> configureChain)
{
_viewTokenFilter = viewTokenFilter;
_configureChain = (chain, token) => configureChain(chain);
}

public ActionLessViewConvention(Func<IViewToken, bool> viewTokenFilter, Action<BehaviorChain, IViewToken> configureChain)
{
_viewTokenFilter = viewTokenFilter;
_configureChain = configureChain;
Expand All @@ -166,7 +184,7 @@ public void Configure(ViewBag bag, BehaviorGraph graph)
var output = token.ToBehavioralNode();
chain.AddToEnd(output);
_configureChain(chain);
_configureChain(chain, token);
});
}
}
Expand Down
@@ -1,5 +1,6 @@
using FubuMVC.Core;
using FubuMVC.Core.Registration;
using FubuMVC.Core.Registration.Routes;
using FubuMVC.Core.View;
using FubuMVC.Tests.Urls;
using FubuMVC.WebForms;
Expand Down Expand Up @@ -43,6 +44,31 @@ public void all_behavior_chains_for_the_action_less_views_should_be_marked_as_pa
}
}

[TestFixture]
public class ActionLessViewRegistrationWithIViewToken
{
private BehaviorGraph theBehaviorGraph;

[SetUp]
public void SetUp()
{
var registry = new FubuRegistry();
registry.Import<WebFormsEngine>();

registry.Views.RegisterActionLessViews(token => token.ViewType.Name.StartsWith("FakeView"), (chain, token) => chain.Route = new RouteDefinition(token.Name));

theBehaviorGraph = registry.BuildGraph();
}

[Test]
public void chain_is_configured_depending_on_token()
{
var behavior = theBehaviorGraph.BehaviorFor(typeof (InputModel));
behavior.Route.Pattern.ShouldEqual(typeof (FakeView1).Name);
}

}

public class FakeView1 : FubuPage<InputModel>{}
public class FakeView2 : FubuPage<TestInputModel> { }
public class FakeView3 : FubuPage<Model1> { }
Expand Down

0 comments on commit 59d7aa3

Please sign in to comment.