- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在设置 LaunchDarkly 以控制我的第一个功能标志及其在服务器和客户端的正常工作。现在我正在尝试 LaunchDarkly Bootstrap 方法(来自下面给出的链接)并尝试像下面我的代码一样,但它不接受双括号而且我不知道如何通过使用 Bootstrap 方法获取标志值,所以我哪里出错了在我的代码中?谁能帮我举个例子吗?
链接,
https://docs.launchdarkly.com/docs/js-sdk-reference#section-bootstrapping
使用 Bootstrap 选项初始化客户端,如下所示,
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");
});
}
最佳答案
完全公开,我叫约翰,我是 LaunchDarkly 支持团队的一员。我很乐意帮助您解决这个问题
首先,您似乎使用的是旧版本的引导示例。新示例修复了拼写错误,并使用了新的 all_flags_state 方法。
我在这里看到两个主要问题。主要问题是如何引导从后端到前端的标志变化,以及如何在使用引导时适本地利用 LaunchDarkly。我将首先解决如何从后端引导标志变体的问题。
LaunchDarkly 文档中的示例利用模板将引导值包含到前端。模板化是一种将以编程方式生成的内容包含在静态源文件或文本文件中的策略。模板通常在编译或部署代码时使用,或者在运行时向客户端提供内容时使用。这样做是为了在最终版本中呈现仅在当时可用的信息。
不同的模板语言有不同的行为方式,但一般来说,您在源文件或文本文件中包含标记,这些标记指示模板渲染器用您提供的数据替换该标记。
在文档中它提到此示例是使用 Ruby 进行模板化的,但该示例使用的是 Mustache 渲染,而 Mustache 可用于许多不同的语言。模板化是一种将以编程方式生成的内容包含在静态源文件或文本文件中的策略。这通常在编译或部署代码时使用,或者在运行时向客户端提供内容时使用。这样做是为了在最终版本中呈现仅在当时可用的信息。
该示例可能无法运行,具体取决于您使用的后端语言和框架。根据与您的问题相关的标签,我可以安全地假设您正在使用 .NET 为您的后端提供支持,它没有规定的模板语言。不过,有许多开源解决方案。
在下面的示例中,我将使用 https://github.com/rexm/Handlebars.Net将用户引导标志值呈现到 result
变量中。我将从 handle bars 存储库中的示例以及 LaunchDarkly 的 hello-bootstrap
和 hello-dotnet
存储库中借用代码,这些代码可在此处获得: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。我看到您每次在评估用户之前都在调用 identify。每次调用 identify SDK 都需要重新初始化。这意味着即使在引导您的初始变体之后,您也将通过调用 identify 强制 SDK 重新初始化,从而消除引导的所有好处。作为解决方案,检测您的用户对象是否已更改。如果有,则调用识别。否则不调用identify,SDK会使用缓存的用户属性。
如果您想更深入地研究这一点并向我们提供更多包装器的源代码,您可以通过 support@launchdarkly.com 联系我们
关于javascript - LaunchDarkly 引导 : (JS) Property Assignment Expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53416842/
在嵌入式系统的背景下,给出以下函数结构: 返回变量的条件赋值: int foo(int x) { int status; if (is_valid(x)) {
#input |--IDs-|--Value-| |--da1-|--100---| |--da2-|---80---| |--da3-|--200---| |--da4-|--300---| |--
您可以declare a list of variables and assign them some value在 Perl 6 my ($a, $b) = 33,44 # $a will be 3
在下面的代码中实现对象的浅拷贝,但是不同的输出让我很困惑: 对象.分配: var obj = { name: 'wsscat', age: 0, add: { a: 'beijin
我正在查看一位已不复存在的开发人员的一些旧代码,并注意到有时他使用 Object.assign({}, xyz) 还有他用过的其他东西 Object.assign([], abc); 两者有区别吗?
这个问题在这里已经有了答案: Why were ES5 Object methods not added to Object.prototype? (2 个答案) 关闭 4 个月前。 我正在做一个
我想知道这之间的区别: Object.assign(otherObject, { someNewProperty: '' }); 和 otherObject.someNewProperty = '
考虑以下代码: const defaultState = () => { return { profile: { id: '', displayName: '',
我刚刚偶然发现this line of TS code : const { title = item.text } = item; 这似乎是一个destructuring assigment但是大括号
我是一个没有经验的 JavaScript 用户,正在阅读这本书 CoffeeScript: Accelerated JavaScript Development ,其中作者制作了一种 Scrabble
当我查看日志文件时 D:\SAS\XXX\Lev1\SASMain\BatchServer\Logs 我看到了这两行 NOTE: Libref TESTLIB successfully assigne
我似乎不明白为什么要使用移动赋值运算符: CLASSA & operator=(CLASSA && other); //move assignment operator 结束了,复制赋值运算符: CL
在Eiffel Studio中,我一直试图访问从另一个类定义的一个类的对象的字段。但是,它不断给出我无法理解和解决的错误。以下是示例代码片段: 创建对象的类: class TEST1 feat
为什么这个片段在 Node (10.5) .then(function() { this = {...this, ...payload}; this.update();
我在我的 React 应用程序中使用以下包来生成 Recaptcha 组件:https://github.com/appleboy/react-recaptcha 组件如下所示,带有 eslint 警
我有以下代码: #include #include using std::cout; struct SomeType { SomeType() {} SomeType(const Some
我有这个代码示例: var a = 10; ({a}) = 0; 在 Google Chrome 中,它显示错误:SyntaxError:无效的解构赋值目标 在 Firefox 中,它显示错误:Ref
我有一个函数,用于对两个输入字段的输入求和并将其分配给一个。我的函数如下所示: function sum(id) { nextId = id+2 console.log
我收到这个警告 "Automatic Reference Counting Issue: Assigning retained object to unsafe_unretained variable
在使用 Shopify 的 Liquid 语言编码时,我注意到使用以下语法分配了一些变量: {%- assign variable = value -%} 和使用以下语法分配的其他变量: {% ass
我是一名优秀的程序员,十分优秀!