我正在设置LaunchDarkly,以控制我的第一个特性标志及其在服务器和客户端的正常工作。现在,我正在尝试LaunchDarkly引导方法(从下面给定的链接),并尝试类似于下面的代码,但是它不接受双大括号,我不知道如何使用引导方法获得标志值,那么我的代码哪里出错了?有人能帮我举个例子吗?
链接,
https://docs.launchdarkly.com/docs/js-sdk-reference#section-bootstrapping使用引导选项初始化客户端,如下所示,
client = LDClient.initialize(sdkKey, userContext.user, options = {
bootstrap: {
{{ ldclient.all_flags(userContext.user) }}
}
});我的函数可以得到标志值,
isFeatureEnabled: function (featureFlag, properties) {
console.log("Before Variation");
//we shall update the custom properties into user context.
if (properties) {
for (var k in properties) {
userContext.user.custom[k] = properties[k];
}
}
//later make the identity call to update the user details.
client.identify(userContext.user, null, function () { /*rules updated*/
console.log("New user's flags available");
//validate the feature flag
var showFeature = client.variation(featureFlag);
if (!showFeature) {
window.in8.platform.showUnauthorized('');
}
console.log("after Variation");
});
}发布于 2018-11-26 23:34:48
坦白说,我叫约翰,我是LaunchDarkly支持团队的一员。我很乐意帮你解决这个问题
首先,您似乎使用了一个较早版本的引导示例。新示例具有一个错误修复,并使用新的all_flags_state方法。
我在这里看到了两个主要问题。主要问题是如何引导从后端到前端的标志变体,以及如何在使用引导时适当地利用LaunchDarkly。我将首先讨论如何从后端引导标志变体的问题。
LaunchDarkly文档中的示例使用模板将引导值包含到前端。模板是一种策略,用于将以编程方式生成的内容包含在静态源文件或文本文件中。模板通常用于编译或部署代码,或在运行时向客户端提供内容。这样做是为了只在最后版本中提供当时可用的信息。
不同的模板语言以不同的方式运行,但通常在源文件或文本文件中包含标记,这些标记指示模板呈现器用提供它的数据替换该令牌。
在文档中,它提到了这个例子是用于使用Ruby的模板,但是这个例子使用的是胡子渲染,而且胡子可以在许多不同的语言中使用。模板是一种策略,用于将以编程方式生成的内容包含在静态源文件或文本文件中。这通常用于编译或部署代码,或在运行时向客户端提供内容。这样做是为了只在最后版本中提供当时可用的信息。
根据所使用的后端语言和框架,该示例可能无法工作。根据与您的问题相关的标记,我可以放心地假设您正在使用.NET为后端供电,因为后端没有规定的模板语言。不过,有很多开源解决方案。
在下面的示例中,我将使用https://github.com/rexm/Handlebars.Net将用户引导的标志值呈现到result变量中。我将从句柄条形回购中的示例以及LaunchDarkly的hello-bootstrap和hello-dotnet repos中借用可用的代码,这些代码可在这里获得:https://github.com/launchdarkly/hello-bootstrap & https://github.com/launchdarkly/hello-dotnet
string source =
@"
<html>
<head>
<script src=""https://app.launchdarkly.com/snippet/ldclient.min.js""></script>
<script>
window.ldBootstrap={{ldBootstrap}};
window.ldClientsideId=""{{ldClientsideId}}"";
window.ldUser={{ldUser}};
</script>
</head>
<body>
<h1>LaunchDarkly server-side bootstrap example</h1>
<ul>
<li><code>normal client</code>: <span class=""normal"">initializing…</span></li>
<li><code>bootstrapped client</code>: <span class=""bootstrap"">initializing…</span></li>
</ul>
<script>
var user = window.ldUser;
console.log(`Clients initialized`);
var client = LDClient.initialize(window.ldClientsideId, user);
var bootstrapClient = LDClient.initialize(window.ldClientsideId, user, {
bootstrap: window.ldBootstrap
});
client.on('ready', handleUpdateNormalClient);
client.on('change', handleUpdateNormalClient);
bootstrapClient.on('ready', handleUpdateBootstrapClient);
bootstrapClient.on('change', handleUpdateBootstrapClient);
function handleUpdateNormalClient(){
console.log(`Normal SDK updated`);
render('.normal', client);
}
function handleUpdateBootstrapClient(){
console.log(`Bootstrapped SDK updated`);
render('.bootstrap', bootstrapClient);
}
function render(selector, targetClient) {
document.querySelector(selector).innerHTML = JSON.stringify(targetClient.allFlags(user), null, 2);
}
</script>
</body>
</html>";
var template = Handlebars.Compile(source);
Configuration ldConfig = LaunchDarkly.Client.Configuration.Default("YOUR_SDK_KEY");
LdClient client = new LdClient(ldConfig);
User user = User.WithKey("bob@example.com")
.AndFirstName("Bob")
.AndLastName("Loblaw")
.AndCustomAttribute("groups", "beta_testers");
var data = new {
ldBootstrap: JsonConvert.SerializeObject(client.AllFlagsState(user)),
ldUser = JsonConvert.SerializeObject(user),
ldClientsideId = "YOUR_CLIENT_SIDE_ID"
};
var result = template(data);您可以以此示例为例,在向用户提供页面时对其进行调整以呈现静态源代码。
第二个问题是如何使用SDK。在每次评估用户之前,我看到您正在打电话给every。每次调用,都需要重新初始化SDK。这意味着,即使在引导您的初始变体之后,您也将通过调用identify来强制SDK重新初始化,从而删除引导的所有好处。作为解决方案,检测您的用户对象是否已更改。如果有,那就打电话标识。否则,不要调用identify,以便SDK使用缓存的用户属性。
如果您想深入研究这个问题,并为您的包装提供更多的源代码,您可以在support@launchdarkly.com上与我们联系。
https://stackoverflow.com/questions/53416842
复制相似问题